Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android Notification之監聽系統清除通知欄方法

Android Notification之監聽系統清除通知欄方法

編輯:關於Android編程

編寫了一個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();
}
}


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved