Android Notification之监听系统清除通知栏方法
2014-09-01 15:51
603 查看
编写了一个Notification通知工具类,里面含有监听系统清除通知栏方法,焦点在加粗斜体部分:
public class Notifier {
private static final String TAG = Notifier.class.getSimpleName();
private static Notifier instance;
private Context mContext;
private static final int NOTIFICATION_ID_1 = 0;
private static final int NOTIFICATION_ID_2 = 1;
private static final int NOTIFICATION_ID_3 = 2;
private static final int NOTIFICATION_ID_4 = 3;
private static final int NOTIFICATION_ID_5 = 4;
private static final int NOTIFICATION_ID_6 = 5;
private static final int NOTIFICATION_ID_7 = 6;
private static final int NOTIFICATION_ID_8 = 7;
public static Notifier getInstance(Context context) {
if (instance == null) {
instance = new Notifier(context);
}
return instance;
}
public Notifier(Context context) {
mContext = context;
}
public NotificationManager getNotificationManager(Context context) {
return (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
}
public void showNotify(Context context, int largeIcon, int smallIcon,
CharSequence tickerText, CharSequence contentInfo,
CharSequence contentTitle, CharSequence contentText) {
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, MainActivity.class), 0);
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
largeIcon);
NotificationCompat.Builder notifyBuilder = new NotificationCompat.Builder(
context);
// notifyBuilder.setLargeIcon(bitmap);
notifyBuilder.setSmallIcon(smallIcon);
notifyBuilder.setTicker(tickerText);
notifyBuilder.setContentInfo(contentInfo);
notifyBuilder.setContentTitle(contentTitle);
notifyBuilder.setContentText(contentText);
notifyBuilder.setAutoCancel(false);
notifyBuilder.setWhen(System.currentTimeMillis());
notifyBuilder.setDefaults(Notification.DEFAULT_ALL);
notifyBuilder.setContentIntent(contentIntent);
Notification noti = notifyBuilder.build();
getNotificationManager(context).notify(NOTIFICATION_ID_1, noti);
}
public void showCustomViewNotify(Context context, int smallIcon,
CharSequence tickerText, int layoutId) {
RemoteViews mRemoteViews = new RemoteViews(context.getPackageName(),
layoutId);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, MainActivity.class), 0);
NotificationCompat.Builder notifyBuilder = new NotificationCompat.Builder(
context);
notifyBuilder.setSmallIcon(smallIcon);
notifyBuilder.setContent(mRemoteViews);
notifyBuilder.setTicker(tickerText);
notifyBuilder.setOngoing(true);
notifyBuilder.setAutoCancel(true);
notifyBuilder.setDefaults(Notification.DEFAULT_ALL);
notifyBuilder.setContentIntent(contentIntent);
Notification noti = notifyBuilder.build();
Intent dismissedIntent = new Intent(
"com.xxxx.android.notify.dismissed");
/**
* The intent to execute when the notification is explicitly dismissed
* by the user, either with the "Clear All" button or by swiping it away
* individually.
*
* This probably shouldn't be launching an activity since several of
* those will be sent at the same time.
*/
noti.deleteIntent = PendingIntent.getBroadcast(context, 0,
dismissedIntent, 0);
getNotificationManager(context).notify(NOTIFICATION_ID_2, noti);
}
public Notification getForegroundServiceNotify(Context context,
int smallIcon, CharSequence tickerText, CharSequence contentInfo,
CharSequence contentTitle, CharSequence contentText) {
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, MainActivity.class), 0);
NotificationCompat.Builder notifyBuilder = new NotificationCompat.Builder(
context);
notifyBuilder.setSmallIcon(smallIcon);
notifyBuilder.setTicker(tickerText);
notifyBuilder.setContentInfo(contentInfo);
notifyBuilder.setContentTitle(contentTitle);
notifyBuilder.setContentText(contentText);
notifyBuilder.setAutoCancel(false);
notifyBuilder.setContentIntent(contentIntent);
// notifyBuilder.setWhen(System.currentTimeMillis());
notifyBuilder.setDefaults(Notification.DEFAULT_ALL);
return notifyBuilder.build();
}
}
public class Notifier {
private static final String TAG = Notifier.class.getSimpleName();
private static Notifier instance;
private Context mContext;
private static final int NOTIFICATION_ID_1 = 0;
private static final int NOTIFICATION_ID_2 = 1;
private static final int NOTIFICATION_ID_3 = 2;
private static final int NOTIFICATION_ID_4 = 3;
private static final int NOTIFICATION_ID_5 = 4;
private static final int NOTIFICATION_ID_6 = 5;
private static final int NOTIFICATION_ID_7 = 6;
private static final int NOTIFICATION_ID_8 = 7;
public static Notifier getInstance(Context context) {
if (instance == null) {
instance = new Notifier(context);
}
return instance;
}
public Notifier(Context context) {
mContext = context;
}
public NotificationManager getNotificationManager(Context context) {
return (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
}
public void showNotify(Context context, int largeIcon, int smallIcon,
CharSequence tickerText, CharSequence contentInfo,
CharSequence contentTitle, CharSequence contentText) {
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, MainActivity.class), 0);
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
largeIcon);
NotificationCompat.Builder notifyBuilder = new NotificationCompat.Builder(
context);
// notifyBuilder.setLargeIcon(bitmap);
notifyBuilder.setSmallIcon(smallIcon);
notifyBuilder.setTicker(tickerText);
notifyBuilder.setContentInfo(contentInfo);
notifyBuilder.setContentTitle(contentTitle);
notifyBuilder.setContentText(contentText);
notifyBuilder.setAutoCancel(false);
notifyBuilder.setWhen(System.currentTimeMillis());
notifyBuilder.setDefaults(Notification.DEFAULT_ALL);
notifyBuilder.setContentIntent(contentIntent);
Notification noti = notifyBuilder.build();
getNotificationManager(context).notify(NOTIFICATION_ID_1, noti);
}
public void showCustomViewNotify(Context context, int smallIcon,
CharSequence tickerText, int layoutId) {
RemoteViews mRemoteViews = new RemoteViews(context.getPackageName(),
layoutId);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, MainActivity.class), 0);
NotificationCompat.Builder notifyBuilder = new NotificationCompat.Builder(
context);
notifyBuilder.setSmallIcon(smallIcon);
notifyBuilder.setContent(mRemoteViews);
notifyBuilder.setTicker(tickerText);
notifyBuilder.setOngoing(true);
notifyBuilder.setAutoCancel(true);
notifyBuilder.setDefaults(Notification.DEFAULT_ALL);
notifyBuilder.setContentIntent(contentIntent);
Notification noti = notifyBuilder.build();
Intent dismissedIntent = new Intent(
"com.xxxx.android.notify.dismissed");
/**
* The intent to execute when the notification is explicitly dismissed
* by the user, either with the "Clear All" button or by swiping it away
* individually.
*
* This probably shouldn't be launching an activity since several of
* those will be sent at the same time.
*/
noti.deleteIntent = PendingIntent.getBroadcast(context, 0,
dismissedIntent, 0);
getNotificationManager(context).notify(NOTIFICATION_ID_2, noti);
}
public Notification getForegroundServiceNotify(Context context,
int smallIcon, CharSequence tickerText, CharSequence contentInfo,
CharSequence contentTitle, CharSequence contentText) {
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, MainActivity.class), 0);
NotificationCompat.Builder notifyBuilder = new NotificationCompat.Builder(
context);
notifyBuilder.setSmallIcon(smallIcon);
notifyBuilder.setTicker(tickerText);
notifyBuilder.setContentInfo(contentInfo);
notifyBuilder.setContentTitle(contentTitle);
notifyBuilder.setContentText(contentText);
notifyBuilder.setAutoCancel(false);
notifyBuilder.setContentIntent(contentIntent);
// notifyBuilder.setWhen(System.currentTimeMillis());
notifyBuilder.setDefaults(Notification.DEFAULT_ALL);
return notifyBuilder.build();
}
}
相关文章推荐
- android之Notification监听系统清除通知栏
- Android 通过系统使用NotificationListenerService 监听各种Notification的使用方法
- Android 通过系统使用NotificationListenerService 监听各种Notification的使用方法
- Android 通过系统使用NotificationListenerService 监听各种Notification的使用方法
- Android 监听系统音乐播放器播放的音乐的方法
- Android 通知Notification的两种实现方法
- 立即获得下载任务完成的消息通知。Android DownloadManager通过注册一个广播监听系统的广播事件完成此操作,在创建广播时候,需要指明过滤器为:DownloadManager.ACTIO
- Android Notification 使用详解之三:通过服务更新进度通知&在Activity中监听服务进度
- Android 监听系统音乐播放器播放的音乐的方法
- android 监听软键盘弹出和隐藏和4.4系统沉浸式通知栏冲突解决
- 主动通知Android系统刷新文件,并对刷新进度做监听
- 【Android】监听Notification被清除
- Android 监听系统音乐播放器播放的音乐的方法
- Android 监听系统音乐播放器播放的音乐的方法
- Android使用ContentObserver监听数据库变化 接受指定号码的短信,并且不让系统截取到通知用户
- Android Notification使用系统通知栏布局出现的图标问题
- Android Demo Sample: 一种新建Notification通知的方法
- android使用Notification进行通知时有时候通知栏中的项背景是灰白的的,而不是根据手机系统的颜色而定
- Android 监听Notification 被清除实例代码
- android点击系统通知Notification,进入指定的activity