我希望以与用户在启动器中点按其图标时完全相同的方式从状态栏通知中恢复我的应用.
那就是:我希望堆栈处于与用户离开之前相同的状态.
在通知上设置待处理意图时的问题是它始终以特定活动为目标.我不想要这个.我需要像启动器那样恢复应用程序.
因此,如果用户处于活动A中,我想恢复活动A.如果他已从活动A启动活动B,那么我希望在用户点击通知时显示B,并且要恢复堆栈以便A获得用户点击B中的后退按钮时恢复
还有其他几个问题与类似的标题,但没有一个解决我的问题.
我观察过几个类似的问题,但他们无法帮助我.我需要在用户点击通知时显示上次活动.这是我的代码:
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(YourService.this)
.setContentTitle(getResources().getText(R.string.app_name))
.setContentText(getServiceStateDescription(YourService.this))
.setSmallIcon(iconId)
.setWhen(System.currentTimeMillis());
Intent nIntent = getPreviousIntent();
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack
stackBuilder.addParentStack(MainActivity_.class);
stackBuilder.addNextIntent(nIntent);
PendingIntent pendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(pendingIntent);
startForeground(ContextConstants.LAUNCHER_SERVICE_NOTE_ID, notificationBuilder.build());
private Intent getPreviousIntent() {
Intent newIntent = null;
final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
final List<ActivityManager.AppTask> recentTaskInfos = activityManager.getAppTasks();
if (!recentTaskInfos.isEmpty()) {
for (ActivityManager.AppTask appTaskTaskInfo: recentTaskInfos) {
if (appTaskTaskInfo.getTaskInfo().baseIntent.getComponent().getPackageName().equals(ContextConstants.PACKAGE_NAME)) {
newIntent = appTaskTaskInfo.getTaskInfo().baseIntent;
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
}
}
} else {
final List<ActivityManager.RecentTaskInfo> …
Run Code Online (Sandbox Code Playgroud) 我的目标是,如果用户点击通知,则返回上一个活动.我以这种方式在服务onCreate()中创建通知:
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Run Code Online (Sandbox Code Playgroud)
但是当我点击通知时,这将转到MainActivity而不是之前打开的最后一个单击主页按钮.
在清单中,我尝试使用launchMode ="singleTop","standard"和"singletask"来使用MainActivity,但没有成功.
谢谢.