[英]Pending intent extras are received only when activity is paused
I'm sending extra string with pending intent from my StateCh.java
to MainActivity
. 我正在从StateCh.java
向MainActivity
发送带有挂起意图的额外字符串。 My expectation on it is to display dialog in MainActivity
when pending intent with extra is arrived (notification is clicked). 我对它的期望是当有额外的待处理意图到达时(单击通知),在MainActivity
显示对话框。 The problem is when i open the MainActivity
and then I click the notification there is no extras inside pending intent and dialog isn't displayed. 问题是,当我打开MainActivity
然后单击通知时,在挂起的intent中没有额外的内容,并且不显示对话框。 When I pause the MainActivity
(by pressing back button) and click the notification again it works as expected. 当我暂停MainActivity
(按后退按钮)并再次单击通知时,它按预期工作。
MainActivity.java: 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: 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);
}
// ...
}
Change Bundle extras = getIntent().getExtras();
Change Bundle extras = getIntent().getExtras();
To Bundle extras = intent.getExtras();
To Bundle extras = intent.getExtras();
or call setIntent(intent) first 或者先调用setIntent(intent)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.