【发布时间】:2012-10-24 13:59:53
【问题描述】:
我有一项服务,当用户执行操作时,它会使用 Service.startForeground(..) 更改为在前台运行,因为杀死它会对用户造成干扰。
我使用以下代码构建一个通知来创建它的待处理意图:
Intent topActivityIntent = new Intent(this, topActivityClass);
// Rebuild the task stack
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack
stackBuilder.addParentStack(topActivityClass);
stackBuilder.addNextIntent(topActivityIntent);
for (int i = 0; i < stackBuilder.getIntentCount(); i++) {
Intent currentIntent = stackBuilder.editIntentAt(i);
// Set flags to tell that we want to re-use the existing
// topActivity if possible
currentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP |
Intent.FLAG_ACTIVITY_NEW_TASK);
}
// Adds the Intent to the top of the stack
// Gets a PendingIntent containing the entire back stack
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
topActivityClass 是用户当前看到的活动,因为我希望他/她回到相同的活动。
这里的问题是,当用户点击通知时,会创建一个新的Activity,而不是使用任务堆栈中的那个。
我已尝试将标志仅设置为 topActivityIntent 和几种标志组合。
我应该使用哪些标志来避免创建新活动?是否可以重用堆栈中的活动?
【问题讨论】:
标签: android android-intent android-service android-notifications