2013-03-27 50 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"));