我正在从我的StateCh.java
向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);
}
// ...
}
答案 0 :(得分:6)
更改Bundle extras = getIntent().getExtras();
至Bundle extras = intent.getExtras();