2013-03-27 13 views
-1

我已經在我的應用程序中設置了通知。它的工作正常。如果我點擊statusbar中的通知,它會發送到我的應用程序。
現在我需要設置一些工作,如果通知被點擊,我可以在哪裏設置? 當通知被點擊時,是否有隱式調用的方法?
另外我想刪除該通知,如果它被點擊,該怎麼做?onclicklistener通知

這是我的代碼

notifManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
Intent inty=getIntent(); 
note = new Notification(R.drawable.icon, "New E-mail", System.currentTimeMillis()); 
PendingIntent intent = PendingIntent.getActivity(MainActivity.this, 0, inty, 0); 
note.setLatestEventInfo(MainActivity.this, "New E-mail", "You have one unread message.", intent); 
notifManager.notify(R.string.search_hint, note); 

回答

1

你可以一些額外的數據添加到意圖,然後在您的活動尋找它在OnCreate和onNewIntent方法。

例如:

inty.putExtra("came from notification", true); 

可以然後通過()通過使用getIntent傳遞給onNewIntent或在的onCreate意圖讀了這一點。

intent.getBooleanExtra("came from notification", false); 
1

嘗試調用廣播接收器它可能對你很有幫助的要求,

Intent notificationIntent = new Intent(this, dummy_activity.class); 
notificationIntent.setAction("android.intent.action.MAIN"); 
notificationIntent.addCategory("android.intent.category.LAUNCHER"); 
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
           notificationIntent, 
           PendingIntent.FLAG_UPDATE_CURRENT | 
           Notification.FLAG_AUTO_CANCEL); 

// Now, once this dummy activity starts send a broad cast to your parent activity and finish the pending activity 
//remember you need to register your broadcast action here to receive. 
BroadcastReceiver call_method = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     String action_name = intent.getAction(); 
     if (action_name.equals("call_method")) { 
      // call your method here and do what ever you want. 
     } 
    } 
}; 
registerReceiver(call_method, new IntentFilter("call_method"));