Search code examples
androidlockscreen

Creating android Lockscreen widgets/notification for an app


I've seen many apps do this. Lux comes to mind, also the music player app, and other apps. They display a notification on the lockscreen that has functionality you can interact with. I read lockscreen widgets have been removed on 5.0+, but I still see these apps creating these lockscreen functionality.

I am new to Android development so maybe I am confused on the terminology. Where can I find info on how to create this type of lockscreen functionality.


Solution

  • HI It can be done using RemoteControlClient part of Android since ICS This part of the code is to intercept media controls.

    @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);
    }
    

    }