我遇到了一个问题:我有一些通知,并且我想向一个活动传递意图附加项,即使应用程序已经在运行。
我在这里找到的一些解决方案对我无效,比如添加:
android:launchMode="singleTop"
除了清单文件,我还在使用这个:
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
像某人建议的那样。
我的代码是这样的:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(NotificationService.this);
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentTitle("New website change.");
mBuilder.setContentText("Changed: " + url);
Intent notificationIntent = new Intent(getContext(), MainActivity.class);
Bundle b = new Bundle();
b.putString("key", url);
notificationIntent.putExtras(b);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(getContext(), id, notificationIntent, 0);
mBuilder.setContentIntent(intent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NotificationService.this.NOTIFICATION_SERVICE);
mNotificationManager.notify(id, mBuilder.build());
然后在活动中:
Bundle b = getIntent().getExtras();
if(b != null) {
url = b.getString("key");
editText.setText(url);
webView.loadUrl(url);
webView.requestFocus();
}
任何帮助都将受到欢迎,我将发布此应用程序的代码作为开源,谢谢。