簡體   English   中英

僅在活動暫停時才會收到待處理的意圖附加內容

[英]Pending intent extras are received only when activity is paused

我正在從StateCh.javaMainActivity發送帶有掛起意圖的額外字符串。 我對它的期望是當有額外的待處理意圖到達時(單擊通知),在MainActivity顯示對話框。 問題是,當我打開MainActivity然后單擊通知時,在掛起的intent中沒有額外的內容,並且不顯示對話框。 當我暫停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();

或者先調用setIntent(intent)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM