Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

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

編輯:關於 Android 程式設計

下面是一個封裝好的 Notification 通知工具類 Notifier,其中包含了一個實用功能:監聽系統清除通知欄(無論是點擊「清除全部」還是單獨滑動移除)的方法。關鍵代碼集中在加粗部分。

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;

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.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");

  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.setDefaults(Notification.DEFAULT_ALL);

  return notifyBuilder.build();

}

}

核心思路在於:為通知設置 deleteIntent,並通過 PendingIntent.getBroadcast 關聯一個自定義的廣播。當用戶清除該通知時,系統便會發送這個廣播,我們只需在對應的廣播接收器中處理即可實現「監聽清除通知欄」的效果。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved