2015-09-04 88 views
0

我已經創建了一個應用程序,以顯示在通知欄推送消息,我試圖一個變量傳遞給第二活動傳通知欄值到另一個活動中機器人

這裏是代碼

public void showBarMassage(int resVal){ 
    final int id1 = resVal; 
    runOnUiThread(new Runnable() { 
     public void run() { 

      Intent myI; 
      PendingIntent myPI; 
      NotificationManager myNM; 
      myI =new Intent(getApplicationContext(),ClaimPage.class); 
      myI.putExtra("id",id1); 
      System.out.println(id1); 
      myI.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      myPI = PendingIntent.getActivity(getApplicationContext(),0,myI,0); 
      myNM =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

      Notification MyN = new Notification(); 
      MyN.icon =R.drawable.ic_launcher; 
      MyN.tickerText = "Loyalty Message"; 
      MyN.number=id1; 
      MyN.defaults |= Notification.DEFAULT_SOUND; 
      MyN.defaults |= Notification.DEFAULT_VIBRATE; 
      MyN.flags |= Notification.FLAG_AUTO_CANCEL; 

      MyN.setLatestEventInfo(

        getApplicationContext(), "Congratulation!!", "You have loyalty reward... ", myPI 
      ); 

      myNM.notify(1, MyN); 

     } 
    }); 
} 

當我訪問第二個活動的值時,它總是變爲空

@Override 
protected void onNewIntent(Intent intent) { 
    Bundle extras = intent.getExtras(); 
    if(extras != null){ 
     if(extras.containsKey("id")) 
     { 
      setContentView(R.layout.activity_claim_page); 
      String msg = extras.getString("id"); 
      System.out.println(msg); 

     } 

這是爲什麼?

回答

0

嘗試這樣的事情,並檢查我測試過這個工程!

Intent notificationIntent = new Intent(context, YourClass); 
    notificationIntent.putExtra("id", yourID); 

    PendingIntent notifyIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 
+0

它不起作用。仍然得到「空」作爲輸出。 – Sajith

+0

檢查我編輯的答案我已測試它應該爲你工作! – Shishram

+0

並通過捆綁,你可以在你的下一個活動中獲得你的額外資源 – Shishram

相關問題