android 通知

https://developer.android.com/design/patterns/notifications.html

通知系统可让用户随时了解应用中的相关和即时事件,例如来自好友的新聊天信息或日历事件。可将通知视作新闻频道,在重要的事件发生时提醒用户注意,或者当作日志,在用户未注意时记录事件—可在用户的所有 Android 设备上按需同步。

Android 5.0 新增内容

在 Android 5.0 中,通知在结构、外观和功能方面获得了重要的更新:

  • 通知在外观上发生了更改,与新的材料设计主题保持一致。
  • 通知现在可以在设备锁定屏幕上使用,而敏感信息仍然可以隐藏于背后。
  • 设备在使用时收到的高优先级通知现在采用名为浮动通知的新格式。
  • 云同步通知:在一台 Android 设备上清除通知,则在其他设备上也会将其清除。

注:该版本 Android 的通知设计与之前的版本大不相同。 有关之前版本通知设计的信息,请参阅 Android 4.4 及更低版本中的通知

通知详解


本部分介绍通知的基本组成部分,及其在不同类型设备上显示的方式。

基本布局

所有通知至少要包括一个基本布局,包括:

  • 通知的图标。图标以符号形式表示来源应用。如果应用生成多个类型的通知,它也可用于指明通知类型。
  • 通知标题以及其他 文本
  • 时间戳

利用 Notification.Builder为之前版本平台创建的通知,其外观和行为方式与在 Android 5.0 中完全相同,唯一的变动在于系统为您处理通知的方式存在细微的样式变动。 如需了解之前 Android 版本通知设计的详细信息,请参阅 Android 4.4 及更低版本中的通知

===============================================================================================================================================

处理兼容性

并非所有通知功能都可用于某特定版本,即便用于设置这些功能的方法位于支持库类 NotificationCompat.Builder 中也是如此。 例如,依赖于扩展通知的操作按钮仅会显示在 Android 4.1 及更高版本的系统中,这是因为扩展通知本身仅在 Android 4.1 及更高版本的系统中可用。

为了确保最佳兼容性,请使用 NotificationCompat 及其子类(特别是 NotificationCompat.Builder)创建通知。此外,在实现通知时,请遵循以下流程:

  1. 为所有用户提供通知的全部功能,无论他们使用何种版本的 Android 系统。 为此,请验证是否可从应用的 Activity 中获得所有功能。要执行此操作,您可能需要添加新的 Activity

    例如,若要使用 addAction() 提供停止和启动媒体播放的控件,请先在应用的 Activity 中实现此控件。

  2. 确保所有用户均可通过点击通知启动 Activity 来获得该Activity中的功能。 为此,请为 Activity 创建 PendingIntent。调用 setContentIntent() 以将 PendingIntent 添加到通知。
  3. 现在,将要使用的扩展通知功能添加到通知。请记住,您添加的任何功能还必须在用户点击通知时启动的 Activity 中可用

在通知中显示进度


通知可能包括动画形式的进度指示器,向用户显示正在进行的操作状态。 如果您可以估计操作所需的时间以及任意时刻的完成进度,则使用“限定”形式的指示器(进度栏)。 如果无法估计操作的时长,则使用“非限定”形式的指示器(Activity 指示器)。

平台的 ProgressBar 类实现中显示有进度指示器。

要在 Android 4.0 及更高版本的平台上使用进度指示器,需调用 setProgress()。对于早期版本,您必须创建包括 ProgressBar 视图的自定义通知布局。

下文介绍如何使用 setProgress() 在通知中显示进度。

显示持续时间固定的进度指示器

要显示限定形式的进度栏,请通过调用 setProgress(max, progress, false) 将进度栏添加到通知,然后发出通知。随着操作继续进行,递增 progress并更新通知。操作结束时, progress 应该等于 max。调用 setProgress() 的常见方法是将 max 设置为 100,然后将 progress 作为操作的“完成百分比”值递增。

您可以在操作完成后仍保留显示进度栏,也可以将其删除。无论哪种情况,都请记住更新通知文本以显示操作已完成。 要删除进度栏,请调用setProgress(0, 0, false)。例如:

...
mNotifyManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle("Picture Download")
    .setContentText("Download in progress")
    .setSmallIcon(R.drawable.ic_notification);
// Start a lengthy operation in a background thread
new Thread(
    new Runnable() {
        @Override
        public void run() {
            int incr;
            // Do the "lengthy" operation 20 times
            for (incr = 0; incr <= 100; incr+=5) {
                    // Sets the progress indicator to a max value, the
                    // current completion percentage, and "determinate"
                    // state
                    mBuilder.setProgress(100, incr, false);
                    // Displays the progress bar for the first time.
                    mNotifyManager.notify(0, mBuilder.build());
                        // Sleeps the thread, simulating an operation
                        // that takes time
                        try {
                            // Sleep for 5 seconds
                            Thread.sleep(5*1000);
                        } catch (InterruptedException e) {
                            Log.d(TAG, "sleep failure");
                        }
            }
            // When the loop is finished, updates the notification
            mBuilder.setContentText("Download complete")
            // Removes the progress bar
                    .setProgress(0,0,false);
            mNotifyManager.notify(ID, mBuilder.build());
        }
    }
// Starts the thread by calling the run() method in its Runnable
).start();

1.https://developer.android.com/reference/android/app/Notification.html

2.https://developer.android.com/reference/android/app/NotificationManager.html  

3.https://developer.android.com/guide/topics/ui/notifiers/notifications.html#CreateNotification

4.https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html

5.https://developer.android.com/training/material/index.html

原文地址:https://www.cnblogs.com/royi123/p/5647883.html