RemoteControlClient在服务中,不显示小部件

时间:2014-07-04 12:47:47

标签: android lockscreen remote-control

我有一个媒体播放器服务,带有用于播放流的自定义解码器。我尝试了在网络上找到的所有内容,用于在屏幕上注册媒体控制按钮,但它们没有显示出来。

该服务的工作原理如下:

  1. onCreate()我做了以下事情:

    audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    Intent buttonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    ComponentName component = new ComponentName(this, MediaControlReceiver.class);
    
    buttonIntent.setComponent(component);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, buttonIntent, 0);
    myRemoteControlClient = new RemoteControlClient(pendingIntent);
    
    audioManager.registerRemoteControlClient(myRemoteControlClient);
    
    int flags = RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS
            | RemoteControlClient.FLAG_KEY_MEDIA_NEXT
            | RemoteControlClient.FLAG_KEY_MEDIA_PLAY
            | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE
            | RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE
            | RemoteControlClient.FLAG_KEY_MEDIA_STOP;
    myRemoteControlClient.setTransportControlFlags(flags);
    
  2. 当服务收到播放消息时,我启动歌曲,并为锁屏按钮调用以下代码:

    int result = audioManager.requestAudioFocus(focusChangeListener,
            AudioManager.STREAM_MUSIC,
            AudioManager.AUDIOFOCUS_GAIN);
    
    if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
        Log.d("RemoteControl", "Status: PLAY"  + result);
        myRemoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
        setRemoteControlMetadata(null, "test", "test", 1);
    }
    
  3. 我也注册了焦点更改侦听器,但没有用。你能帮我把我的应用程序连接到widget按钮吗?

0 个答案:

没有答案