Android 当用户点击锁屏上的通知时启动活动

Android 当用户点击锁屏上的通知时启动活动,android,notifications,android-pendingintent,lockscreen,Android,Notifications,Android Pendingintent,Lockscreen,我希望能够在设备锁定时点击通知,并在不解锁设备的情况下启动活动 我在onCreate()方法中为活动添加了一些标志,允许在设备锁定时显示活动: Window window = this.getWindow(); window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); window.add

我希望能够在设备锁定时点击通知,并在不解锁设备的情况下启动活动

我在
onCreate()
方法中为活动添加了一些标志,允许在设备锁定时显示活动:

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);
这是创建通知的代码:

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);
我还向清单中添加了
showOnLockScreen=“true”

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

正在工作的事情:

  • 设备锁定时会显示该活动(例如,如果我将该活动留在前台并锁定手机,则该活动将保持在前台,而无需解锁手机)
  • 如果我在手机锁定时点击通知,它会要求我解锁手机,然后显示活动
我希望能够执行相同的操作,但无需解锁设备。
我错过了什么?

试试这个

private NotificationCompat.Builder mBuilder;

Intent notifyIntent = new Intent(getApplicationContext(), MainActivity.class);
    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, notifyIntent, 0);
    mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(getNotificationIcon())
            .setContent(remoteViews)
            .setContentIntent(pendingIntent)
            .setOnlyAlertOnce(true)
            .setOngoing(true);
这是在设备5.0及更低版本上获取通知图标的方法

private int getNotificationIcon() {
    boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
    return useWhiteIcon ? R.drawable.notification_icon : R.mipmap.ic_launcher;
}
删除onCreate中的代码

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);
试试这个

private NotificationCompat.Builder mBuilder;

Intent notifyIntent = new Intent(getApplicationContext(), MainActivity.class);
    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, notifyIntent, 0);
    mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(getNotificationIcon())
            .setContent(remoteViews)
            .setContentIntent(pendingIntent)
            .setOnlyAlertOnce(true)
            .setOngoing(true);
这是在设备5.0及更低版本上获取通知图标的方法

private int getNotificationIcon() {
    boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
    return useWhiteIcon ? R.drawable.notification_icon : R.mipmap.ic_launcher;
}
在onCreate中删除下面的代码

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

remoteViews
变量未声明(也未分配)。我尝试了其余设置,但无法解决我的问题。您确定此代码允许在不解锁设备的情况下从锁屏上的通知启动活动吗?notifyIntent.setFlags(Intent.FLAG_activity_CLEAR_TOP | Intent.FLAG_activity_SINGLE_TOP | Intent.FLAG_activity_CLEAR_TASK | Intent.FLAG_activity_NEW TASK);
remoteViews
变量未声明(也未分配)。我尝试了其余设置,但无法解决我的问题。您确定此代码允许在不解锁设备的情况下从锁屏上的通知启动活动吗?notifyIntent.setFlags(Intent.FLAG_activity_CLEAR_TOP | Intent.FLAG_activity_SINGLE_TOP | Intent.FLAG_activity_CLEAR_TASK | Intent.FLAG_activity_NEW TASK);