0

I've got some problem with sendKeyDownUpSync. I want to use it in my widget to control inbuilt music player. It works almost correctly. Almost because when i call function:

public void previousTruck()
    {
        final Instrumentation inst = new Instrumentation();
        new Thread(new Runnable() {         
            public void run() { 
                   new Instrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_MEDIA_PREVIOUS);

        }).start();     
    }

It start changing music from my playlist but lots of time, why? I want to make only one "step" to next song from play list.

PatLas
  • 179
  • 1
  • 5
  • 16

1 Answers1

2

Somewhere in the OnCreate

myThread = new Thread()
{
    public void run()
    {
        Instrumentation m_Instrumentation = new Instrumentation();
        m_Instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_MEDIA_PREVIOUS);
    }
};

Somewhere in your previousTruck

myThread.start();

P.S. Be attentive!! Creating Thread object without the run() invoking causes the memory leak - see. It happens during changing of screen orientation if you create a thread object inside the OnCreate and there is not the run() invoking till the next changing of screen orientation. That is why the best approach is to create Thread object immediately before it's start() invoking.

Andrew
  • 227
  • 2
  • 4
  • 10