I want to be able to tap on a notification when the device is locked and launch an activity without unlocking the device.
I added some flags to the activity in the onCreate()
method that allow the activity to be displayed when the device is locked:
Window window = this.getWindow(); window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
This is the code that creates the notification:
Intent intent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity( this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); Notification notification = new Notification.Builder(this) .setContentIntent(pendingIntent) .setContentTitle("Title") .setSmallIcon(android.R.drawable.ic_menu_more) .build(); NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); notificationManager.notify(1, notification);
I also added showOnLockScreen="true"
to the manifest:
<activity android:name=".MainActivity" android:label="@string/app_name" android:showOnLockScreen="true" android:theme="@style/AppTheme.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
Things that are working:
- The activity is shown when the device is locked (for example, if I leave the activity on foreground and lock the phone the activity remains on foreground without the need of unlocking the phone)
- If I tap the notification when the phone is locked, it asks me to unlock it and then the activity is shown
I want to be able to do the same but without unlocking the device.
What am I missing?