0

我对sendKeyDownUpSync有一些问题。我想在我的小部件中使用它来控制内置的音乐播放器。它几乎可以正常工作。几乎是因为当我调用函数时:

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

        }).start();     
    }

它开始从我的播放列表中更改音乐,但是很多时间,为什么?我只想从播放列表中向下一首歌曲迈出一个“步骤”。

4

1 回答 1

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.

于 2012-10-15T19:36:46.967 回答