Android 通知的简单使用

2019-06-19  本文已影响0人  酷酷的Demo

如何创建通知

随着Android系统不断升级,Notification的创建方式也随之变化,主要变化如下:

Android 3.0之前

Android 3.0 (API level 11)之前,使用new Notification()方式创建通知:

NotificationManager mNotifyMgr = 
      (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(
      this, 0, new Intent(this, ResultActivity.class), 0);

Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(this, title, content, contentIntent);

mNotifyMgr.notify(NOTIFICATIONS_ID, notification);

Android 3.0 (API level 11)及更高版本

Android 3.0开始弃用new Notification()方式,改用Notification.Builder()来创建通知:

NotificationManager mNotifyMgr = 
      (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(
      this, 0, new Intent(this, ResultActivity.class), 0);

Notification notification = new Notification.Builder(this)
            .setSmallIcon(R.drawable.notification_icon)
            .setContentTitle("My notification")
            .setContentText("Hello World!")
            .setContentIntent(contentIntent)
            .build();// getNotification()

mNotifyMgr.notify(NOTIFICATIONS_ID, notification);

这里需要注意: "build()" 是Androdi 4.1(API level 16)加入的,用以替代
"getNotification()"。API level 16开始弃用"getNotification()"

通知基本用法

NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.notification_icon)
            .setContentTitle("My notification")
            .setContentText("Hello World!");

NotificationCompat.Builder自动设置的默认值:
priority: PRIORITY_DEFAULT
when: System.currentTimeMillis()
audio stream: STREAM_DEFAULT

Intent resultIntent = new Intent(this, ResultActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(
            this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
Notificatioin notification = mBuilder.build();
// Sets an ID for the notification
int mNotificationId = 001;

// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr = 
      (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, notification);
NotificationManager mNotifyMgr = 
      (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Sets an ID for the notification, so it can be updated
int notifyID = 1;
NotificationCompat.Builder mNotifyBuilder = 
      new NotificationCompat.Builder(this)
          .setContentTitle("New Message")
          .setContentText("You've received new messages.")
          .setSmallIcon(R.drawable.ic_notify_status);

int numMessages = 0;
...
    mNotifyBuilder.setContentText("new content text")
            .setNumber(++numMessages);
    mNotifyMgr.notify(notifyID, mNotifyBuilder.build());
...

点击通知栏的清除按钮,会清除所有可清除的通知
设置了 setAutoCancel() 或 FLAG_AUTO_CANCEL的通知,点击该通知时会清除它
通过 NotificationManager 调用 cancel() 方法清除指定ID的通知
通过 NotificationManager 调用 cancelAll() 方法清除所有该应用之前发送的通知

通知类型

通知有两种视图:普通视图和大视图。
普通视图:


image

大视图:
[图片上传失败...(image-f0e56-1560913700779)]

默认情况下为普通视图,可通过NotificationCompat.Builder.setStyle()设置大视图。

注: 大视图(Big Views)由Android 4.1(API level 16)开始引入,且仅支持Android 4.1及更高版本。

上一篇 下一篇

猜你喜欢

热点阅读