通知栏单击以关闭应用程序

时间:2012-02-10 03:44:54

标签: android android-intent android-notification-bar

当用户点击通知栏中的文字而不显示任何窗口时,如何关闭应用程序?

2 个答案:

答案 0 :(得分:1)

使用getBroadcast

创建通知

Intent intent = new Intent("action_close_app");

PendingIntent closeIntent = PendingIntent.getBroadcast(context, requestCode, intent, flags)

当您点击通知时,会发送包含操作action_close_app的广播,您需要注册一个广播接收器来处理action_close_app接收,只需关闭您的应用。

答案 1 :(得分:1)

使用以下代码并在您的活动中注册广播接收器:

NotificationManager mNotificationManager = (NotificationManager)_context.getSystemService(Context.NOTIFICATION_SERVICE);   

Notification notification = new Notification(R.drawable.icon,"",System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL;


Context context = _context.getApplicationContext();


/** Set the intent extras to be passed to calling activity*/
Intent notificationIntent = new Intent("action_close_app");



/** Create a pending intent, requires to generate a notification */

PendingIntent contentIntent = PendingIntent.getBroadcase(
  _context.getApplicationContext(), requestCode,
  notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);


/** Set notification with required fields */

notification.setLatestEventInfo(context, "", "", contentIntent);



/** notify manager to generate notification on status bar*/

mNotificationManager.notify(NOTIFICATION_ID, notification)