我有一个相当简单的应用程序,它接受用户的输入,然后将其设置为通知。用户可以根据需要创建任意数量的通知。我希望用户单击通知并转到名为 ResultActivity
的新 Activity 。 ResultActivity
依次从通知 Intent 中读取 putExtras
并将其显示给用户。下面的代码允许我做我想做的几乎所有事情,除了任何时候按下通知,我都会收到最后创建的通知的 putExtra
。
Intent notificationIntent = new Intent(ctx, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(ctx, i,notificationIntent,PendingIntent.FLAG_CANCEL_CURRENT);
NotificationManager nm = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
Resources res = ctx.getResources();
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(res,R.drawable.ic_launcher))
.setTicker("Remember to " + text.getText())
.setWhen(System.currentTimeMillis()).setAutoCancel(true)
.setContentTitle(text.getText());
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.class);
String pass = text.getText().toString();
resultIntent.putExtra("title", pass);
resultIntent.putExtra("uid", i);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
new Uri.Builder().scheme("data").appendQueryParameter("text", "my text").build();
builder.setContentIntent(resultPendingIntent);
Notification n = builder.build();
n.flags = Notification.FLAG_NO_CLEAR;
nm.notify(i++, n);
text.setText(null);
打开应用
输入“一”
点击确定
通知已发送
打开应用
输入“二”
点击确定
通知已发送
现在您有两个通知。一个写“一”,一个写“二”。如果您单击通知“二”,它会将您带到一个显示“二”的屏幕。完美!
如果您点击通知“一”,它会将您带到一个显示“二”的屏幕。坏了!
ResultActivity.java
public class ResultActivity extends Activity {
String title = null;
TextView text;
int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
text = (TextView) findViewById(R.id.textView1);
title = getIntent().getStringExtra("title");
i = getIntent().getIntExtra("uid", 0);
text.setText(title);
}
最佳答案
我知道这是很久以前的事了,但我觉得答案并没有说明您代码中的问题。
所以问题几乎就在这里
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
因此,您从带有 update_current 标志的 stackbuilder 创建了一个 pendingIntent。如果您查看 FLAG_UPDATE_CURRENT,它会显示
/**
* Flag indicating that if the described PendingIntent already exists,
* then keep it but replace its extra data with what is in this new
* Intent. For use with {@link #getActivity}, {@link #getBroadcast}, and
* {@link #getService}. <p>This can be used if you are creating intents where only the
* extras change, and don't care that any entities that received your
* previous PendingIntent will be able to launch it with your new
* extras even if they are not explicitly given to it.
*/
public static final int FLAG_UPDATE_CURRENT = 1<<27;
那么在您的用例中发生的情况是,您从 stackbuilder 创建了两个相同的 pendingintents,并且第二个 Intent 覆盖了第一个 Intent 。实际上你永远不会创建第二个你只是更新第一个的额外内容。
很遗憾,您的用例没有可用的标志,但有一个很好的破解方法。您可以做的是使用 resultIntent 的 setAction 并放置一个随机字符串或对您的应用有意义的字符串。
例如。 resultIntent.setAction("dummy_action_"+ notification.id);
这将使您的 resultIntent 足够独特,以便 pendingIntent 将创建它而不是更新之前的一个。
关于java - Android 多重通知和多重 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12968280/