在Android中设置锁定屏幕背景(如Spotify do)

Nif*_*hel 23 android

我知道这个主题已经在这里和这里讨论了,答案似乎是不可能的.

但我最近在我的Nexus 4(4.4.2)中安装了Spotify,似乎有可能.当我在Spotify中听一首歌时,锁屏背景会改变我正在收听的专辑的封面(见截图).

我的理论是:当手机被锁定时,他们会用专辑封面更改手机壁纸,以便更改锁定屏幕背景,然后当手机解锁时,它们会退回前一个.但这不是他们如何做到的,因为在Spotify的权限列表中没有"android.permission.SET_WALLPAPER"...... :(

他们是如何做到的呢?一些理论?

屏幕截图锁屏 屏幕截图锁屏

Kai*_*Kai 12

编辑: 下面的解决方案仅适用于已注册为媒体控制器的应用程序,因此不播放音频的应用程序不能/不应使用此机制来更改锁屏壁纸.


它可以使用RemoteControlClient完成,这是Android自ICS以来的一部分.如果您想要一个有效的示例,请下载适用于Android的VLC并查看org.videolan.vlc.AudioService:

这部分代码是拦截媒体控件.

/**
 * Set up the remote control and tell the system we want to be the default receiver for the MEDIA buttons
 * @see http://android-developers.blogspot.fr/2010/06/allowing-applications-to-play-nicer.html
 */
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void setUpRemoteControlClient() {
    Context context = VLCApplication.getAppContext();
    AudioManager audioManager = (AudioManager)context.getSystemService(AUDIO_SERVICE);

    if(Util.isICSOrLater()) {
        audioManager.registerMediaButtonEventReceiver(mRemoteControlClientReceiverComponent);

        if (mRemoteControlClient == null) {
            Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
            mediaButtonIntent.setComponent(mRemoteControlClientReceiverComponent);
            PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(context, 0, mediaButtonIntent, 0);

            // create and register the remote control client
            mRemoteControlClient = new RemoteControlClient(mediaPendingIntent);
            audioManager.registerRemoteControlClient(mRemoteControlClient);
        }

        mRemoteControlClient.setTransportControlFlags(
                RemoteControlClient.FLAG_KEY_MEDIA_PLAY |
                RemoteControlClient.FLAG_KEY_MEDIA_PAUSE |
                RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS |
                RemoteControlClient.FLAG_KEY_MEDIA_NEXT |
                RemoteControlClient.FLAG_KEY_MEDIA_STOP);
    } else if (Util.isFroyoOrLater()) {
        audioManager.registerMediaButtonEventReceiver(mRemoteControlClientReceiverComponent);
    }
}
Run Code Online (Sandbox Code Playgroud)

这部分是为了更新艺术品,以及其他信息:

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private void updateRemoteControlClientMetadata() {
    if(!Util.isICSOrLater()) // NOP check
        return;

    if (mRemoteControlClient != null) {
        MetadataEditor editor = mRemoteControlClient.editMetadata(true);
        editor.putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, getCurrentMedia().getAlbum());
        editor.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, getCurrentMedia().getArtist());
        editor.putString(MediaMetadataRetriever.METADATA_KEY_GENRE, getCurrentMedia().getGenre());
        editor.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, getCurrentMedia().getTitle());
        editor.putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, getCurrentMedia().getLength());
        editor.putBitmap(MetadataEditor.BITMAP_KEY_ARTWORK, getCover());
        editor.apply();
    }
}
Run Code Online (Sandbox Code Playgroud)