1

我構建包含按鈕的自定義通知,並且當用戶按下按鈕時我想列出它。從通知中實現setOnClickPendingIntent

該按鈕不應該打開任何活動,但只有邏輯人員喜歡改變歌曲。

代碼:

 RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification); 
     contentView.setTextViewText(R.id.toptext, nowPlayingTitle); 

     //this not work 
     Intent intent = new Intent(this, receiver.class); 
     intent.putExtra("UNIQ", "1"); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(
       this.getApplicationContext(), 234324243, intent, PendingIntent.FLAG_CANCEL_CURRENT) 
     contentView.setOnClickPendingIntent(R.id.imageButtonPlay, 
       pendingIntent); 
     notification.contentView = contentView; 

     // this is to return to my activity if click somwhere else in notification 
     Intent notificationIntent = new Intent(this, MYACTIVITY.class); 
     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
     notification.contentIntent = contentIntent; 

     mNotificationManager.notify(NOTIFICATION_ID, notification); 

我沒有得到自己需要在第二個PARAM的setOnClickPendingIntent的竅門? 如何在用戶按下按鈕後調用函數?

IM可能失去了一些東西,因爲我不明白的接收方和哪些用戶按

回答

3

你缺少的事實,你實際上創建的按鈕不屬於您的應用程序後happend。它是在另一個環境中,在另一個過程中創建的。它無法調用你的函數。

而是,當用戶點擊該按鈕時,該未決意圖被觸發。您可以通過接收器(在您的活動中)捕捉它,檢查一些參數並執行操作。 或者你可以實現一個服務並在後臺處理這個意圖。我更喜歡這種方式。

0

感謝您快速回答。我嘗試使用接收器,但它從未解僱。 的代碼是主要的問題,我的reciever類創建下面的代碼:

public class receiver extends BroadcastReceiver 
{ 
    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
    Bundle bundle = intent.getExtras(); 
    }  
} 

,但點擊該通知,永遠不會觸發接收器(在調試模式下測試)

+0

好吧,我明白了。錯誤在我的清單:) – gilush14

+0

清單中的問題是什麼? – amarkovits