简体   繁体   中英

Start activity over lockscreen from notification intent

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM