簡體   English   中英

Android消息對象PendingIntent,獲取空Intent.getExtras()。getString(“ mystring”)

[英]Android message object PendingIntent, getting null Intent.getExtras().getString(“mystring”)

我在點擊底部欄時在活動內調用片段。 該片段在片段中具有以下消息對象。

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {

      Intent moreSelectionIntent = new Intent(getActivity(), MainActivity.class);
      moreSelectionIntent.putExtra("selection",Integer.toString(position));
     PendingIntent pendingIntent = PendingIntent.getActivity(getActivity().getApplicationContext(), 0, moreSelectionIntent, PendingIntent.FLAG_UPDATE_CURRENT);
     }        ` 

該片段調用活動並傳遞PendingIntent。 當我使用下面的代碼從意圖中獲取字符串時,我在活動中得到NULL以下是活動中的代碼

 Intent i = this.getIntent();
    Log.i(TAG, "intenet------->"+i);

    String moreSelection = i.getExtras().getString("selection");
    Log.i(TAG, "moreSelection Bundle------->"+moreSelection);

    if(moreSelection!=null)
    { System.out.println("message"+moreSelection);}

您必須添加額外的字符串才能獲取它。 首先將它添加到您的pendingIntent。

使用此代碼...

    Intent intent = new Intent(this, NextActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
    PendingIntent.FLAG_ONE_SHOT);

暫無
暫無

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

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