题
我创建具有类似于以下的东西的通知:
Intent ni = new Intent(this, SomeActivity.class);
ni.putExtra("somestring", "somedata");
PendingIntent contentIntent =
PendingIntent.getActivity(this, 0, ni,
PendingIntent.FLAG_UPDATE_CURRENT|PendingIntent.FLAG_ONE_SHOT);
Context context = getApplicationContext();
notification.setLatestEventInfo(context,
res.getString(R.string.app_name),
text, contentIntent);
这里的关键是在意图用于通知的额外数据。当我点击该通知,并带来了SomeActivity,它带来了活动和额外的数据是可用的。
不过,如果我退出该应用程序,按住home键,直到最近的活动列表中出现,并选择再次打开应用程序,额外的数据仍然传递。有没有一种方法,我可以使这个数据获得通过的只有的,如果应用程序是通过通知打开,而不是从最近的活动列表?
解决方案
像本人看起来可以检查,看看是否活动从最近的活动对话框启动了这一点:
if((getIntent().getFlags() &
Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
// ignore extras
}
else {
// do not ignore extras
}
我不知道这是不是最好的方式,但它确实工作。
其他提示
使用不同的Intent
。例如,你可以添加一个<intent-filter>
一些自定义操作字符串,并用它为基于Notification
-PendingIntent
。换句话说,你不能改变的最近期的活动对话框,但你可以改变你的冲突的一面。
尝试用:
int NOTIFICATION_CODE = 1;
PendingIntent contentIntent =
PendingIntent.getActivity(this,
NOTIFICATION_CODE,
ni,
PendingIntent.FLAG_UPDATE_CURRENT |
PendingIntent.FLAG_ONE_SHOT
);
对于每个通知变更NOTIFICATION_CODE值。
不隶属于 StackOverflow