Launch activity when user taps on a notification from the lockscreen

大憨熊 提交于 2019-12-12 17:06:35

问题


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?


回答1:


Try this!!!

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

this is method get notification icon on device 5.0 and lower

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

Remove code below in 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);


来源:https://stackoverflow.com/questions/37245288/launch-activity-when-user-taps-on-a-notification-from-the-lockscreen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!