当用户点击通知栏中的文字而不显示任何窗口时,如何关闭应用程序?
答案 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)