Set lock screen background in Android (like Spotify do)

前端 未结 6 770
旧巷少年郎
旧巷少年郎 2020-12-02 12:31

I know that this topic has been already discussed here, here and here, and the answer seems to be that it is not possible.

But I recently installed Spotify in my Ne

6条回答
  •  伪装坚强ぢ
    2020-12-02 13:10

    I know this is late but perfect answer is still required. So to set lock screen background in Android (like Spotify do) we have to perform following steps.

    1. set media session active mSession.setActive(true). if session is not active so it is not gona show.

    2. set playback state

    playBackStateBuilder = new PlaybackStateCompat.Builder()
                .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_SKIP_TO_NEXT
                        | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS
                        | PlaybackStateCompat.ACTION_STOP | PlaybackStateCompat.ACTION_PLAY_PAUSE);`
    
    `mSession.setPlaybackState(playBackStateBuilder.setState(PlaybakStateCompate.STATE_PLAYING, 0, 0).build());
    

    Note: lock screen image is showed when first playback state is set to playing then it can be switch to other states

    3. set meta data

    mSession.setMetadata(new MediaMetadataCompat.Builder()
                    .putString(MediaMetadataCompat.METADATA_KEY_TITLE, title)
                    .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, artist)
                    .putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, bitmap)
                    .build());
    

    here KEY_ALBUM_ART is required because this is the image which is shown on lock screen.

    By setting above three things it had showed on my galaxy device but not on pixels devices so for that follow last point.

    4. show notification with media style

    NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, CHANNEL_ID);
        builder.setStyle(
                new androidx.media.app.NotificationCompat.MediaStyle()
        );
        
        mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(121, builder.build());
    

提交回复
热议问题