1

How do I start an activity over the lock screen after clicking on a notification? Whatever I do it asks for my password and starts the activity after I unlock. It doesn't even get to the onCreate methods.

    <activity
        android:name=".Locktask"
        android:configChanges="orientation"
        android:excludeFromRecents="true"
        android:noHistory="true"
        android:showOnLockScreen="true"
        android:showWhenLocked="true"
        android:taskAffinity=""
        android:exported="true"
        android:screenOrientation="portrait"
        android:launchMode="singleTask"
        android:theme="@style/AppTheme.NoActionBar" />


    Intent intent = new Intent(this, Locktask.class);

    PendingIntent pi = PendingIntent.getActivity(this, 2, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel("locknote",
                "locknote name",
                NotificationManager.IMPORTANCE_HIGH);
        channel.setDescription("shows layout over lock.");
        channel.enableLights(true);
        channel.setLightColor(Color.RED);
        channel.enableVibration(true);
        channel.setShowBadge(false);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        channel.setVibrationPattern(new long[] { 0,0 });
        channel.enableVibration(false);
        if (mNotificationManager != null) {
            mNotificationManager.createNotificationChannel(channel);
        }
    }
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "locknote")

            .setContentTitle("w")
            .setContentText("e")
            .setContentIntent(pi)
            .setSmallIcon(R.drawable.ic_action_lock_notification)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT);

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);


    notificationManager.notify(2, mBuilder.build());

Not a duplicate as that answer's permissions have already been added.

Edit: SOVLED

For any poor souls in the future, the notification has to have a custom layout with setCustomContentView() and a onclickpendingintent set to that view. The pending intent should point to an intent service, it's the only way intents can get handled from notifications while still locked.

user2430112
  • 86
  • 1
  • 8
  • So would you like to start the activity by programmatically inserting your password the startup.? – Ümañg ßürmån Oct 08 '18 at 17:29
  • @UmangBurman No I want to start the activity over the lock screen without unlocking the phone. So when the activity is closed again the phone is still locked. – user2430112 Oct 08 '18 at 17:30
  • I don't think it is possible. Do you think of an example app with this behavior? – Onur D. Oct 08 '18 at 17:59
  • check answers from here: https://stackoverflow.com/q/35356848/8354184 – Onur D. Oct 08 '18 at 18:08
  • @OnurD. I have and the problem is the notification doesn't seem to send the pending intent until the phone is unlocked. – user2430112 Oct 08 '18 at 18:26
  • @user2430112 see my answer below – Onur D. Oct 08 '18 at 18:51
  • Hello, can you post the solution? I am in the same problem. thanks – Schwarz54 Dec 10 '19 at 11:37
  • 1
    @Schwarz54 This was a while ago but the key things that got it to work for me were: you HAVE to use a custom layout for the notification and you also HAVE to extend 'IntentService' in whatever your intent target is. So if you have `Intent intent = new Intent(this, Locktask.class);` Locktask must extend IntentService. – user2430112 Dec 10 '19 at 23:15
  • And then from intentService you load the activity calling other intent like: `Intent intent = new Intent(this,Example.class); startActivity(intent)` did you remember is it that? – Schwarz54 Dec 11 '19 at 09:09
  • Here have my question if you want to take a look https://stackoverflow.com/questions/59283626/start-an-activity-from-notification-over-the-secure-lockscreen – Schwarz54 Dec 11 '19 at 10:26

1 Answers1

0

Try this:

    Intent intent = new Intent(this, Locktask.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pi = PendingIntent.getActivity(this, 2, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Onur D.
  • 515
  • 3
  • 11