skip to Main Content

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);

enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    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.


  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search