通知

来源:互联网 发布:vb.net开发工具 编辑:程序博客网 时间:2024/05/17 23:42

通知


通知时你可以在应用常规的UI外部向用户显示的消息.当你告知系统发出通知时,它将先以图标的形式展示在状态栏中,用户可以通过下拉通知栏产看详细的通知信息.状态栏和下拉通知栏均有系统控制.

创建通知


通过NotificationCompat.Builder对象指定UI信息和操作.要创建通知对象,通过NotificationCompat.Builder.build(),它返回一个Notification对象,要发出通知,还需要使用NotificationManager的notify将Notification对象发送给系统.

要发送通知必须设置以下内容

  • 小图标,通过setSmallIcon()设置

  • 标题,通过setContentTitle()设置

  • 详细文本,通过setContentText()设置

通知还可以设置时间(setWhen()),大图标(setLargeIcon(Bitmap bitmap)),声音,震动等,这些都是可选的,上面的三项是必选的,不设置,通知就不会显示.

给通知添加动作

一个通知可以提供多个操作.应该始终定义一个当用户点击通知的时候会触发的操作.还可以给通知添加按钮来执行其他操作.例如:暂停闹铃,回复短信等,此功能在Android4.1后可用.
在Notification内部,操作本身由PendingIntent定义,它包含在应用启动Activity的Intent,将PendingIntent和Notification关联,调用NotificationCompat.Builder对象的setContentIntent(),将PendingIntent和通知绑定,在用户点击通知的时候会触发这个Intent.
PendingIntent对象可以绑定的时Activity,Service,BroadcastReceiver(需要广播类进行接收).例如:音乐播放器,点击通知栏里面的按钮,可以绑定一个广播,当点击了按钮,就发送一个广播,然后在广播接收器里面进行拦截,对接收的广播进行处理.

通知优先级

可以根据需要设置通知的优先级.优先级充当一个提示,提醒设备UI应该如何显示通知.要设置通知的优先级,调用NotificationCompat.Builder.setPriority(NotificationCompat.PRIORITY_MIN),有五个优先级常量.

public static final int PRIORITY_DEFAULT = 0;public static final int PRIORITY_LOW = -1;public static final int PRIORITY_MIN = -2;public static final int PRIORITY_HIGH = 1;public static final int PRIORITY_MAX = 2;

浮动通知

在Android5.0(API 21)之后,当设备处于活跃状态时(设备为上锁且屏幕时打开状态),通知可以显示在小型的浮动窗口上中(浮动通知)

触发浮动通知的条件

  • 用户的Activity处于全屏模式中或者通知具有较高的优先级并使用铃声或者震动.(两个条件满足之一就会触发该效果)
 Notification notification = builder.setContentTitle("title")                .setContentText("content")                .setWhen(System.currentTimeMillis())                .setSmallIcon(R.mipmap.ic_launcher)            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))                .setContentIntent(intent)                .setNumber(numMessages)                .setPriority(NotificationCompat.PRIORITY_MAX)// 这里设置的优先级要高一点,我设置的是最高                .setDefaults(Notification.DEFAULT_ALL)// 声音和震动设为默认

通知栏进度条

manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);        builder = new NotificationCompat.Builder(this);        builder.setSmallIcon(R.mipmap.ic_launcher)                .setContentTitle("downLoad")                .setContentText("download in progress");        new Thread(new Runnable() {            @Override            public void run() {                for (int i = 0; i <= 100; i++) {                    builder.setProgress(100, i, false);                    manager.notify(1, builder.build());                    try {                        Thread.sleep(100);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }                // setProgress()方法中的参数1设为0是进度条不显示                builder.setContentText("complete")                        .setProgress(0, 0, false);                manager.notify(1, builder.build());            }        }).start();
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);        builder = new NotificationCompat.Builder(this);        builder.setSmallIcon(R.mipmap.ic_launcher)                .setContentTitle("downLoad")                .setContentText("download in progress");        new Thread(new Runnable() {            @Override            public void run() {                for (int i = 0; i <= 100; i++) {                    // 类似于安装应用时进度条一直在加载                    builder.setProgress(0, 0, true);                    manager.notify(2, builder.build());                    try {                        Thread.sleep(100);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }                // setProgress()方法中的第一个参数设为0进度条不显示                builder.setContentText("complete")                        .setProgress(0, 0, false);                manager.notify(2, builder.build());            }        }).start();    }

锁屏通知

随着 Android 5.0(API 级别 21)的发布,通知现在还可显示在锁定屏幕上。您的应用可以使用此功能提供媒体播放控件以及其他常用操作。 用户可以通过“设置”选择是否将通知显示在锁定屏幕上,并且您可以指定您应用中的通知在锁定屏幕上是否可见。

设置可见性

通过setVisivility()指定一下值之一:

  • VISIBILITY_PUBLIC 显示通知的完整内容

  • VISIBILITY_SECRET 不会再锁定屏幕上显示此通知的任何部分

  • VISIBILITY_PRIVATE 显示通知图标和内容标题等基本信息,但是隐藏通知的完整内容

在锁定屏幕上控制媒体播放

在 Android 5.0(API 级别 21)中,锁定屏幕不再基于 RemoteControlClient(现已弃用)显示媒体控件。取而代之的是,将 Notification.MediaStyle 模板与 addAction() 方法结合使用,后者可将操作转换为可点击的图标。

manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);builder = new NotificationCompat.Builder(this);builder.setVisibility(Notification.VISIBILITY_PRIVATE)       .setSmallIcon(R.mipmap.ic_launcher)       .addAction(R.mipmap.icon_green_arrow_up, "", null)       .addAction(R.mipmap.icon_green_arrow_down, "", null)       .addAction(R.mipmap.icon_green_arrow_down, "", null)       .setContentText("My Awesome Band")       .setContentTitle("Wonderful Music")       .setLargeIcon(bitmap))manager.notify(3, builder.build());

自定义通知布局

可以利用通知框架定义自定义通知布局,有该布局定义通知在RemoteViews对象中的外观.自定义布局通知类似于常规通知,但是它们是基于XML布局文件中定义的RemoteViews.
自定义通知布局的高度取决于通知视图.普通视图布局显示为64dp,扩展视图布局限制为256dp
定义自定义通知布局,首先要实例化RemoteViews对象来扩充XML布局文件,然后调用setContent(),而不是调用setContentTitle()方法.
获取RemoteViews对象:

RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.my_widght); // 创建一个RemoteViews布局// 给布局内的控件设值 remoteViews.setImageViewIcon(); remoteViews.setTextViewText();
0 0
原创粉丝点击