56

我正在嘗試使用新的通知界面。我在通知中添加了3個按鈕,並且每次單擊它們時我都會將某些內容保存到我的數據庫中。確定Android通知的addAction點擊

通知本身運行良好,並在被調用時顯示,我只是不知道如何捕獲三個不同按鈕點擊中的每一個。

我使用BroadcastReceiver來捕捉點擊,但我不知道如何判斷哪個按鈕被點擊。

這是AddAction代碼(我已經排除該通知的其餘部分,因爲它的運作良好) -

//Yes intent 
    Intent yesReceive = new Intent(); 
    yesReceive.setAction(CUSTOM_INTENT); 
    Bundle yesBundle = new Bundle();    
    yesBundle.putInt("userAnswer", 1);//This is the value I want to pass 
    yesReceive.putExtras(yesBundle); 
    PendingIntent pendingIntentYes = PendingIntent.getBroadcast(this, 12345, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT); 
    mBuilder.addAction(R.drawable.calendar_v, "Yes", pendingIntentYes); 

    //Maybe intent 
    Intent maybeReceive = new Intent(); 
    maybeReceive.setAction(CUSTOM_INTENT); 
    Bundle maybeBundle = new Bundle();    
    maybeBundle.putInt("userAnswer", 3);//This is the value I want to pass 
    maybeReceive.putExtras(maybeBundle); 
    PendingIntent pendingIntentMaybe = PendingIntent.getBroadcast(this, 12345, maybeReceive, PendingIntent.FLAG_UPDATE_CURRENT); 
    mBuilder.addAction(R.drawable.calendar_question, "Partly", pendingIntentMaybe); 

    //No intent 
    Intent noReceive = new Intent(); 
    noReceive.setAction(CUSTOM_INTENT); 
    Bundle noBundle = new Bundle();    
    noBundle.putInt("userAnswer", 2);//This is the value I want to pass 
    noReceive.putExtras(noBundle); 
    PendingIntent pendingIntentNo = PendingIntent.getBroadcast(this, 12345, noReceive, PendingIntent.FLAG_UPDATE_CURRENT); 
    mBuilder.addAction(R.drawable.calendar_x, "No", pendingIntentNo); 

這是BroadcastReceiver的代碼 -

public class AlarmReceiver extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 
    Log.v("shuffTest","I Arrived!!!!"); 
    //Toast.makeText(context, "Alarm worked!!", Toast.LENGTH_LONG).show(); 

    Bundle answerBundle = intent.getExtras(); 
    int userAnswer = answerBundle.getInt("userAnswer"); 
    if(userAnswer == 1) 
    { 
     Log.v("shuffTest","Pressed YES"); 
    } 
    else if(userAnswer == 2) 
    { 
     Log.v("shuffTest","Pressed NO"); 
    } 
    else if(userAnswer == 3) 
    { 
     Log.v("shuffTest","Pressed MAYBE"); 
    } 

}   
} 

我已經在清單中註冊了BroadcastReceiver。 另外,我想提到的是,當我單擊通知中的某個按鈕時,會調用BroadcastReceiver,但意圖總是包含額外的'2'。

這是notifcation iteslf - notification

+0

這是奇怪。我在代碼中看不到任何錯誤。如果你改變addAction()的順序(和PendingIntent創建),你還會得到'2'嗎? – Quanturium 2013-03-12 01:24:39

+0

在這種情況下,它只是給了我最後一個addAction()我叫 – Tofira 2013-03-12 08:59:46

+0

我可以爲三個按鈕中的每一個註冊三個BroadcastReceivers,但我真的想避免這種情況。 – Tofira 2013-03-12 09:32:18

回答

103

這是因爲你使用FLAG_UPDATE_CURRENT與具有相同的作用

從文檔意圖:

如果所描述的PendingIntent已經存在,然後保留它,但是用這個新的Intent替換它的額外數據。

當您指定pendingIntentMaybependingIntentNo,系統將使用pendingIntentYes創建PendingIntent,但它覆蓋的花絮。因此,所有三個變量都指向相同的對象,並且最後指定的附加值爲pendingIntentNo

您應該爲每個Intent指定一個替代操作。你仍然可以有一個BroadcastReceiver,並且只需要攔截所有三個動作。這將是減少混亂語義以及:)

您的通知海報:

//Yes intent 
Intent yesReceive = new Intent(); 
yesReceive.setAction(YES_ACTION); 
PendingIntent pendingIntentYes = PendingIntent.getBroadcast(this, 12345, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT); 
mBuilder.addAction(R.drawable.calendar_v, "Yes", pendingIntentYes); 

//Maybe intent 
Intent maybeReceive = new Intent(); 
maybeReceive.setAction(MAYBE_ACTION); 
PendingIntent pendingIntentMaybe = PendingIntent.getBroadcast(this, 12345, maybeReceive, PendingIntent.FLAG_UPDATE_CURRENT); 
mBuilder.addAction(R.drawable.calendar_question, "Partly", pendingIntentMaybe); 

//No intent 
Intent noReceive = new Intent(); 
noReceive.setAction(NO_ACTION); 
PendingIntent pendingIntentNo = PendingIntent.getBroadcast(this, 12345, noReceive, PendingIntent.FLAG_UPDATE_CURRENT); 
mBuilder.addAction(R.drawable.calendar_x, "No", pendingIntentNo); 

接收機:

@Override 
public void onReceive(Context context, Intent intent) { 
    String action = intent.getAction(); 

    if(YES_ACTION.equals(action)) { 
     Log.v("shuffTest","Pressed YES"); 
    } else if(MAYBE_ACTION.equals(action)) { 
     Log.v("shuffTest","Pressed NO"); 
    } else if(NO_ACTION.equals(action)) { 
     Log.v("shuffTest","Pressed MAYBE"); 
    } 
}   
+1

我有一個疑問,我需要清除。我正在使用一個簡單的服務類來啓動,暫停和停止服務。因此,如果單擊即暫停圖像或vise-verse,則會有一個圖像按鈕將更改其圖像的可繪製性。在上面的代碼中,您提到了包含drawable的mBuilder.addAction。我無法弄清楚圖像的id在哪裏?有可能有兩個可繪製的按鈕,但只能用於一個圖像按鈕。 – 2013-07-18 21:09:48

+2

我在YES_ACTION.equals(動作)中遇到錯誤原因?如何解決這個問題。 – 2013-07-19 06:24:09

+0

在哪裏以及如何設置YES_ACTION,NO_ACTION ...等等...那麼顯性文件看起來像 – Cripto 2013-12-29 05:35:42

10
在我的情況下,它加入的意圖過濾

後爲我工作

<receiver android:name=".AlarmReceiver"> 
     <intent-filter> 
      <action android:name="YES_ACTION"/> 
      <action android:name="NO_ACTION"/> 
      <action android:name="MAYBE_ACTION"/> 
     </intent-filter> 
    </receiver> 
+1

最佳做法(y) – 2015-12-31 12:59:43

2

here YES_ACTION必須是yourfullpackagename.YES

private static final String YES_ACTION = "com.example.packagename.YES"; 

同樣可以使用NO_ACTIONMAYBE_ACTION

在廣播接收器必須使用相同的YES_ACTION如上聲明,

意味着廣播接收器類,你可以檢查定製的廣播以下

public class NotificationReceiver extends BroadcastReceiver { 

private static final String YES_ACTION = "com.example.packagename.YES"; 
@Override 
public void onReceive(Context context, Intent intent) { 
    // TODO Auto-generated method stub 
    String action = intent.getAction(); 
    if(YES_ACTION.equals(action)) { 
     Toast.makeText(context, "CALLED", Toast.LENGTH_SHORT).show(); 
    } 
} 

}

注意:在YES_ACTION字符串中,您也可以使用其他字。

8

STEP_BY_STEP

步驟1

public void noto2() // paste in activity 
{ 
    Notification.Builder notif; 
    NotificationManager nm; 
    notif = new Notification.Builder(getApplicationContext()); 
    notif.setSmallIcon(R.drawable.back_dialog); 
    notif.setContentTitle(""); 
    Uri path = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    notif.setSound(path); 
    nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

    Intent yesReceive = new Intent(); 
    yesReceive.setAction(AppConstant.YES_ACTION); 
    PendingIntent pendingIntentYes = PendingIntent.getBroadcast(this, 12345, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT); 
    notif.addAction(R.drawable.back_dialog, "Yes", pendingIntentYes); 


    Intent yesReceive2 = new Intent(); 
    yesReceive2.setAction(AppConstant.STOP_ACTION); 
    PendingIntent pendingIntentYes2 = PendingIntent.getBroadcast(this, 12345, yesReceive2, PendingIntent.FLAG_UPDATE_CURRENT); 
    notif.addAction(R.drawable.back_dialog, "No", pendingIntentYes2); 



    nm.notify(10, notif.getNotification()); 
} 

步驟1.5

我創建全局類AppConstant

public class AppConstant 
    { 
    public static final String YES_ACTION = "YES_ACTION"; 
    public static final String STOP_ACTION = "STOP_ACTION"; 
    } 

步驟2:

public class NotificationReceiver extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 
    // TODO Auto-generated method stub 
    String action = intent.getAction(); 
    if (AppConstant.YES_ACTION.equals(action)) { 
     Toast.makeText(context, "YES CALLED", Toast.LENGTH_SHORT).show(); 
    } 
    else if (AppConstant.STOP_ACTION.equals(action)) { 
     Toast.makeText(context, "STOP CALLED", Toast.LENGTH_SHORT).show(); 
    } 
} 

}

步驟3

<receiver android:name=".NotificationReceiver"> 
     <intent-filter> 
      <action android:name="YES_ACTION"/> 
      <action android:name="STOP_ACTION"/> 

     </intent-filter> 
    </receiver> 
+0

這樣做的工作嗎?你有沒有測試過這個? – 2017-03-22 06:25:15

+0

是的,它工作的很好,我剛剛測試過它 – ysfcyln 2017-04-29 13:18:12

+0

工作正常,我試過了,沒有問題... – Venkat 2017-11-29 08:13:45