当前位置: 代码迷 >> 综合 >> RemoteViews
  详细解决方案

RemoteViews

热度:36   发布时间:2023-12-16 11:34:26.0

要点

在这里插入图片描述

RemoteViews 简介

开发中经常使用的通知栏就是RemoteViews。主要通过NotificationManager的notify来实现。然而RemoteViews还可以用来做桌面小部件,主要通过AppWidgetProvider来实现。这两者更新界面时无法像在activity里面那样更新。因为二者都是运行在其他进程中(系统的SystemService进程)但是remoteview提供了一系列的set方法便于更新。

通知栏

1、让人迷惑的三个类
  • Notification :
    通知的重要类,不推荐使用,很多方法移除。推荐使用Notification.Builder。
  • Notification.Builder :
    builder模式,帮助快速构建通知(推荐使用)
  • NotificationCompat.Builder
    Compat兼容包,提供兼容,解决Notification.Builder的兼容问题(力荐使用)
2、简单栗子(普通的通知)
   @RequiresApi(api = Build.VERSION_CODES.O) 8.0 适配public void notification(View view) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this,"1"); //参数2 channelIdbuilder.setSmallIcon(R.mipmap.ic_launcher); // 设置小图标 参数:资源id(设置大图标传bitmap)builder.setContentTitle("这是标题");       // 设置标题builder.setContentText("这是正文");       //设置正文builder.setSubText("这是摘要");          // 设置摘要builder.setAutoCancel(true);            // 点击通知后是否清除通知builder.setTicker("我在状态栏时显示"); //通知在状态栏上时显示的文字builder.setPriority(NotificationCompat.PRIORITY_MAX);//设置优先级。优先级范围:[-2,2]builder.setWhen(System.currentTimeMillis()); // 时间builder.setOngoing(true); // 通知正在进行时,用户是否能清除通知Intent intent = new Intent(this,SecondActivity.class);PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);builder.setContentIntent(pendingIntent);NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);// 活动managersetChannel(mNotificationManager);mNotificationManager.notify(1, builder.build()); // 发送通知}/**** 设置通知的渠道* */@RequiresApi(api = Build.VERSION_CODES.O) // 8.0 适配private void setChannel(NotificationManager mNotificationManager){
    //channelId 要与上文一致 如上的new NotificationCompat.Builder(this,"1")NotificationChannel channel = new NotificationChannel("1", "Channel1", NotificationManager.IMPORTANCE_DEFAULT);channel.enableLights(true); //是否在桌面icon右上角展示小红点channel.setLightColor(Color.GREEN); //小红点颜色channel.setShowBadge(true); //是否在久按桌面图标时显示此渠道的通知mNotificationManager.createNotificationChannel(channel);}

注意点:
1、安卓8.0以后要求你的app有不同渠道(类型)通知,用户可以选择接受 你的app的那些类型的通知。
2、不同channel通知栗子参考:高德地图。(设置-应用管理-高德地图-通知管理)
渠道详情参考:
1、官方文档安卓8.0新特性-通知栏的channel
2、Android通知栏微技巧,8.0系统中通知栏的适配

3、RemoteViews类的使用自定义通知
/*** 自定义通知*/@RequiresApi(api = Build.VERSION_CODES.O)private void customNotification() {
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "1");builder.setSmallIcon(R.mipmap.ic_launcher);RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.activity_second);remoteViews.setTextViewText(R.id.tv_title, "自定义通知");builder.setContent(remoteViews);setChannel(mNotificationManager);mNotificationManager.notify(1, builder.build());}

如上:
1、通过RemoteViews类来自定义通知布局
2、自定义时需要app包名,和自定义的布局id
3、更新view时不能直接更新,只能通过RemoteViews提供的相关api更新。(一般我们通过findViewById更新,但是这个方法Remoteviews并没有提供)
更多通知使用参考:Android Notification的使用

桌面小部件

RemoteViews另一个应用就是桌面小部件。

参考:https://allen-yu.com/2018/11/25/安卓之RemoteViews的学习/