0

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

4

2 回答 2

1

创建通知使用getBroadcast

Intent intent = new Intent("action_close_app");

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

当你点击通知时,将发送一个带有动作的广播action_close_app,你需要注册一个广播接收器来处理action_close_app接收,只需关闭你的应用程序。

于 2012-02-10T04:11:16.450 回答
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)
于 2012-02-10T05:46:15.890 回答