从通知意图通过锁屏开始活动

时间:2018-10-08 17:24:49

标签: android

单击通知后如何在锁定屏幕上启动活动?无论我做什么,都会要求输入密码并在解锁后开始活动。它甚至没有涉及onCreate方法。

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

不是重复的,因为已经添加了该答案的权限。

编辑:已解决

对于将来的任何可怜的人,通知必须具有一个自定义布局,其中包含setCustomContentView()并为此视图设置了onclickpendingintent。待处理的意图应该指向一个意图服务,这是在仍处于锁定状态时可以从通知中处理意图的唯一方法。

1 个答案:

答案 0 :(得分:0)

尝试一下:

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