Notification bar click to close app

隐身守侯 提交于 2019-12-04 21:11:39

Use the following code and register broadcast receiver in your activity :

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)

create the notification use getBroadcast

Intent intent = new Intent("action_close_app");

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

when you click the notification, will send a broadcast with the action action_close_app, you need register a broadcast receiver to process action_close_app receive, just close your app.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!