2010-06-13 82 views
1

我試圖完成的是通過通知管理器 發送一個通知,一旦點擊就會在應用程序中執行某些操作,只有當它正在運行時。 我曾嘗試使用:PendingIntent從一個通知發送

notification.contentIntent = PendingIntent.getActivity(this, nNotificationCounter, Someintent, PendingIntent.FLAG_NO_CREATE) 

哪個八方通導致異常一次嘗試使用通知。 我切換到:

Notification notification = new Notification(icon, tickerText, when); 

RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.some_notification); 
contentView.setTextViewText(R.id.title, sTitle); 
contentView.setTextViewText(R.id.text, sText); 
notification.contentView = contentView; 
notification.defaults |= Notification.DEFAULT_SOUND; 
notification.number = nNotificationCounter; 

Intent notificationIntent = new Intent(this, MainWindow.class).setAction(ACTION_RESET_MESSGE_COUNTER); 
notification.contentIntent = PendingIntent.getBroadcast(this, nNotificationCounter, notificationIntent, 0); 

,雖然這個代碼不會導致異常。它不叫我的廣播接收器,其定義如下:

public class IncomingReceiver extends BroadcastReceiver { 
      @Override 
      public void onReceive(Context context, Intent intent) { 
       if (intent.getAction().equals(ACTION_RESET_MESSGE_COUNTER)) { 
        System.out.println("GOT THE INTENT"); 
        return; 
       } 

      } 
     } 

,並在OnCreate設置:

IntentFilter filter = new IntentFilter(ACTION_RESET_MESSGE_COUNTER); 
     IncomingReceiver receiver = new IncomingReceiver(); 
     context.registerReceiver(receiver, filter); 

有誰看到一些錯誤的代碼? 或者當通知被點擊時我將如何獲取消息,但是如果尚未創建任何活動,則不會創建任何活動。

編輯:添加了意圖創建和通知創建。

感謝, 湯姆

+0

確保'notificationIntent'設置正確。既然你沒有在這裏包括它的代碼,我們不能提供很多幫助。 – CommonsWare 2010-06-13 15:36:04

+0

我添加了所有的代碼,通常是這種方式來獲得一個事件,只有當通知運行acticity? – totem 2010-06-13 15:45:56

回答

0

你最有可能沒有得到廣播,因爲你是正確創建IntentIntent(Context, Class<?>)告訴系統創建一個「創建」該類的意圖。換句話說,這個意圖直接僅限於硬編碼類。只需使用new Intent(String)或在您的特定情況下new Intent(ACTION_RESET_MESSGE_COUNTER),您將創建我稱之爲「廣播」意圖或文檔稱爲「操作」意圖的內容。這樣,意圖將針對正在傾聽特定廣播/動作的任何人。