I want to programmatically switch a music track, but this code does not work in the background, how to solve this problem?
Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_MEDIA_NEXT);
CodePudding user response:
You need to get an instance of ModuleAndroidKeyboard then call start() on that.
public class ModuleAndroidKeyboard extends Thread {
@Override
public void run() {
Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_MEDIA_NEXT);
}
}
ModuleAndroidKeyboard module = new ModuleAndroidKeyboard();
module.start(); // this line executes the codes of run()
To learn more about Thread in Java, visit W3Schools
CodePudding user response:
I figured it out, I had to sign the apk with a system certificate and register INJECT_EVENTS in the manifest, after which everything worked.

