只有当活动暂停时,才会接收到挂起的意图



我正在发送额外的字符串,从我的StateCh.javaMainActivity。我对它的期望是在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);
    }
    // ...      
}

Change Bundle extras = getIntent().getExtras();

To Bundle extras = intent.getExtras();

最新更新