Android 仅当活动暂停时,才会接收挂起的意图附加
我正在从我的Android 仅当活动暂停时,才会接收挂起的意图附加,android,android-pendingintent,Android,Android Pendingintent,我正在从我的StateCh.java向MainActivity发送带有挂起意图的额外字符串。我对它的期望是,当带有额外的待定意图到达时(单击通知),在MainActivity中显示对话框。问题是,当我打开main活动然后单击通知时,在挂起的意图中没有额外内容,并且对话框不显示。当我暂停main活动(按后退按钮)并再次单击通知时,它将按预期工作 MainActivity.java: public class MainActivity extends Activity { //... @O
StateCh.java
向MainActivity
发送带有挂起意图的额外字符串。我对它的期望是,当带有额外的待定意图到达时(单击通知),在MainActivity
中显示对话框。问题是,当我打开main活动
然后单击通知时,在挂起的意图中没有额外内容,并且对话框不显示。当我暂停main活动
(按后退按钮)并再次单击通知时,它将按预期工作
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);
}
// ...
}
更改Bundle extras=getIntent().getExtras()代码> To
Bundle extras=intent.getExtras()代码>
或者先打电话谢谢!我不敢相信它这么简单,但它成功了。