2016-02-01 32 views
1

我已創建自定義通知。在這個通知中我有一個按鈕。我的問題是,當我點擊按鈕,我似乎無法將任何額外的新課程傳遞給新課程。捆綁包始終爲空

這是我使用的按鈕代碼:

// notification button 
Intent switchIntent = new Intent(context, SecondClass.class); 
switchIntent.putExtra("passThis", passThisValue); 
PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(context, 0, switchIntent, 0); 
contentView.setOnClickPendingIntent(R.id.button, pendingSwitchIntent); 

類我想要的演員:

包是空百達。如果您需要完整的代碼我將編輯我的帖子,我只發佈了代碼中我認爲問題所在的部分。

我沒有正確地做到這一點?我應該如何將值傳遞給通知按鈕事件上的類?

回答

1

試試這個代碼

Bundle bundle = new Bundle(); 
bundle.putString("passThis", passThisValue); 
switchIntent.putExtras(bundle); 
+0

這是工作,但我覺得很奇怪,因爲我已經嘗試這個問題。唯一的區別是,現在我先卸載應用程序,進行更改並重新安裝。那麼,工作......謝謝! – Ravers

+0

@Raverrs謝謝..你可以接受這個作爲你的答案。它有助於。 – thedarkpassenger

0

如果您想從通知中獲取傳遞的Bundle將onNewIntent重寫爲您的活動,如下面的代碼所示。希望這個幫助。

@Override 
protected void onNewIntent(Intent intent) { 
    super.onNewIntent(intent); 

    Bundle extras = intent.getExtras(); 
    if(extras!=null) { 
     String id = extras.getString("passThis"); 
    } 
} 
0

我做了一個關於這個教程要審查我的情況下的一個鄰居。這裏的link

1

上面的代碼犯規傳遞數據包其剛準備爲目的的額外....你必須調用

getIntent().getStringExtra("YOURKEY");