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);
}
這是爲什麼?
它不起作用。仍然得到「空」作爲輸出。 – Sajith
檢查我編輯的答案我已測試它應該爲你工作! – Shishram
並通過捆綁,你可以在你的下一個活動中獲得你的額外資源 – Shishram