我正在从我StateCh.java
的 to发送带有待处理意图的额外字符串MainActivity
。我对它的期望是在MainActivity
带有额外的待处理意图到达时显示对话框(单击通知)。问题是当我打开MainActivity
然后单击通知时,未决意图中没有额外内容,并且不显示对话框。当我暂停MainActivity
(按后退按钮)并再次单击通知时,它按预期工作。
MainActivity.java:
public class MainActivity extends Activity {
//...
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Bundle extras = getIntent().getExtras();
if(extras !=null) {
String value1 = extras.getString("message");
Log.v("alert", value1);
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("title");
alertDialog.setMessage(value1);
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//startActivity(MainActivity.this.getIntent());
}
});
alertDialog.show();
}
}
}
StateCh.java:
public class StateCh extends Service {
//...
private void notificationU(String title, String text) {
//The intent to launch when the user clicks the expanded notification
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("message", "something");
intent.setAction("actionstring" + System.currentTimeMillis());
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification noti2 = new NotificationCompat.Builder(this)
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(R.drawable.warning)
.setContentIntent(pendIntent)
.build();
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(123456, noti2);
}
// ...
}