我想現在調試此爲年齡,我似乎無法找出問題所在:通知的PendingIntent硬錯誤(或很簡單的一個)
我有一個廣播接收器,該接收廣播成功。 通知有兩個動作(「按鈕」):從演員
firstIntent = null;
secondIntent = null;
firstPendingIntent = null; //first "button" to join with the first intent
secondPendingIntent = null; //second "button" to join with the second intent
if(boolean){
//relevant
firstIntent = new Intent(getBaseContext(), NotificationFunctions.class).putExtra("action", "do_this");
secondIntent = new Intent(getBaseContext(), NotificationFunctions.class).putExtra("action", "do_that");
}else{
firstIntent = new Intent(getBaseContext(), NotificationFunctions.class).putExtra("action", "do_another_this");
secondIntent = new Intent(getBaseContext(), NotificationFunctions.class).putExtra("action", "do_another_that");
}
firstPendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0, firstIntent, PendingIntent.FLAG_UPDATE_CURRENT);
secondPendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0, secondIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification = new NotificationCompat.Builder(getApplicationContext())
.setContentTitle(notification_title)
.setContentText(notification_text)
.setTicker("Notification!")
.setWhen(System.currentTimeMillis())
.setDefaults(Notification.DEFAULT_SOUND)
.addAction(R.drawable.abc_cab_background_top_holo_light, first_option, firstPendingIntent)
.addAction(R.drawable.abc_cab_background_top_holo_dark, second_option, secondPendingIntent)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_stat_name)
.build();
每當我調試的的BroadcastReceiver,出於某種原因,行動始終記錄「do_that」,即使我點擊第一個或通知的第二個按鈕。有什麼理由呢?我似乎不明白爲什麼。
public class NotificationFunctions extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
boolean feedback;
String action = intent.getExtras().getString("action");
Log.wtf("...", action); //logs do_that
}}
<3。你能解釋一下爲什麼使用'this'代替getBaseContext或getApplicationContext? – 2014-09-13 13:08:00
@RichardNorton:我做了「只是解釋」。例如,'getBaseContext()'很大程度上是沒有記錄的。而且,它是'Activity'上的一個方法,它是''Context''。當你已經有一個'Context'使用時,爲什麼你會使用一個未記錄的方法返回一些你不知道它是什麼的'Context'?除此之外,我會建議[Dave Smith的博客文章](http://www.doubleencore.com/2013/06/context/)介紹不同來源的Context的作用。 – CommonsWare 2014-09-13 13:14:19