Android中级——RemoteView

RemoteView

  • RemoteView的应用
    • Notification
    • Widget
    • PendingIntent
  • RemoteViews内部机制
  • 模拟RemoteViews

RemoteView的应用

Notification

如下开启一个系统的通知栏,点击后跳转到某网页

public class MainActivity extends AppCompatActivity {private static final String TAG = "MainActivity";protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);NotificationChannel channel1 = new NotificationChannel("0", "channel1", NotificationManager.IMPORTANCE_LOW);manager.createNotificationChannel(channel1);Notification.Builder builder = new Notification.Builder(this, "0");builder.setSmallIcon(R.drawable.ic_launcher).setLargeIcon(Icon.createWithResource(this, R.drawable.ic_launcher)).setContentTitle("Notification").setContentText("Hello World").setContentIntent(pendingIntent);manager.notify(1, builder.build());}
}

效果如下

在这里插入图片描述

若采用RemoteView,可以自定义通知栏的布局,notification.xml文件如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/root"android:layout_width="match_parent"android:layout_height="match_parent"><ImageViewandroid:id="@+id/iv"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1" /><TextViewandroid:id="@+id/tv"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center_vertical" />
</LinearLayout>

代码如下,可通过一系列set方法设置布局

public class MainActivity extends AppCompatActivity {private static final String TAG = "MainActivity";protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);NotificationChannel channel1 = new NotificationChannel("0", "channel1", NotificationManager.IMPORTANCE_LOW);manager.createNotificationChannel(channel1);RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification);remoteViews.setTextViewText(R.id.tv, "Hello Android");remoteViews.setImageViewResource(R.id.iv, R.drawable.ic_launcher);remoteViews.setOnClickPendingIntent(R.id.root, pendingIntent);Notification.Builder builder = new Notification.Builder(this, "0");builder.setSmallIcon(R.drawable.ic_launcher).setCustomContentView(remoteViews);manager.notify(1, builder.build());}
}

效果如下

在这里插入图片描述

Widget

res/layout下创建widget.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><ImageViewandroid:id="@+id/iv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/ic_launcher" />
</LinearLayout>

res/xml下创建appwidget_provider_info.xml,设置最小宽高、自动更新的周期(ms)

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"android:initialLayout="@layout/widget"android:minWidth="84dp"android:minHeight="84dp"android:updatePeriodMillis="86400000"></appwidget-provider>

创建MyAppWidgetProvider,给小组件设置一个点击动画

  • onEnable:第一次添加时调用,可添加多次,但只在第一次调用
  • onUpdate:添加或更新(周期时间到)时调用
  • onDeleted:删除时调用
  • onDisabled:最后一个小组件被删除时调用
  • onReceive :分发上面的事件
public class MyAppWidgetProvider extends AppWidgetProvider {private static final String TAG = "MyAppWidgetProvider";public static final String CLICK_ACTION = "com.demo.demo0.MyAppWidgetProvider.CLICK";public MyAppWidgetProvider() {super();}@Overridepublic void onReceive(Context context, Intent intent) {super.onReceive(context, intent);String action = intent.getAction();Log.d(TAG, "onReceive: action = " + action);if (CLICK_ACTION.equals(action)) {Toast.makeText(context, "click", Toast.LENGTH_SHORT).show();new Thread(new Runnable() {@Overridepublic void run() {Bitmap srcBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);for (int i = 0; i < 37; i++) {float degree = (i * 10) % 360;RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);remoteViews.setImageViewBitmap(R.id.iv, rotateBitmap(context, srcBitmap, degree));/*Intent clickIntent = new Intent();clickIntent.setAction(CLICK_ACTION);clickIntent.setComponent(new ComponentName(context, "com.demo.demo0.MyAppWidgetProvider"));PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, clickIntent, 0);remoteViews.setOnClickPendingIntent(R.id.iv, pendingIntent);*/appWidgetManager.updateAppWidget(new ComponentName(context, MyAppWidgetProvider.class), remoteViews);SystemClock.sleep(30);}}}).start();}}@Overridepublic void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {super.onUpdate(context, appWidgetManager, appWidgetIds);Log.d(TAG, "onUpdate: ");int count = appWidgetIds.length;Log.d(TAG, "onUpdate: count = " + count);for (int appWidgetId : appWidgetIds) {onWidgetUpdate(context, appWidgetManager, appWidgetId);}}private void onWidgetUpdate(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {Log.d(TAG, "onWidgetUpdate: appWidgetId = " + appWidgetId);RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);Intent intentClick = new Intent();intentClick.setAction(CLICK_ACTION);intentClick.setComponent(new ComponentName(context, "com.demo.demo0.MyAppWidgetProvider"));PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentClick, 0);remoteViews.setOnClickPendingIntent(R.id.iv, pendingIntent);appWidgetManager.updateAppWidget(appWidgetId, remoteViews);}private Bitmap rotateBitmap(Context context, Bitmap srcBitmap, float degree) {Matrix matrix = new Matrix();matrix.reset();matrix.setRotate(degree);return Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(), srcBitmap.getHeight(), matrix, true);}
}

AppWidgetProvider本质是一个广播,需要在Manifest中注册,第二个Action是桌面组件的标识必须要加

<receiverandroid:name=".MyAppWidgetProvider"><meta-dataandroid:name="android.appwidget.provider"android:resource="@xml/appwidget_provider_info" /><intent-filter><action android:name="com.demo.demo0.MyAppWidgetProvider.CLICK" /><action android:name="android.appwidget.action.APPWIDGET_UPDATE" /></intent-filter>
</receiver>

PendingIntent

PendingIntent是在将来的某个不确定的时刻发生,而Intent是立刻发生

在这里插入图片描述

PendingIntent通过send和cancel方法发送和取消特定的Intent

  • requesetCode一般情况下设为0
  • 当ComponentName和intent-filter相同时,两个Intent相同
  • 当Intent和requestCode相同时,两个PendingIntent相同

Flag常用的有:

  • FLAG_ONE_SHOT:当前PendingIntent只能被调用一次,随后被自动cancel,后续send会调用失败。对于消息来说,后续通知和第一条通知保持一致,单击任一条通知后,其他无法再打开
  • FLAG_NO_CREATE:当前PendingIntent不会主动创建,若之前不存在,则调用上面方法返回null
  • FLAG_CANCEL_CURRENT:若当前PendingIntent已存在,则会被cancel并创建新的,被cancel的通知再点击无作用。对于消息来说,只有最新的才能打开
  • FLAG_UPDATE_CURRENT:若当前PendingIntent已存在,则更新Intent中的Extra。对于消息来说,前面通知和最后一条通知保持一致,且都可以打开

RemoteViews内部机制

RemoteViews用于在其他进程中显示并更新UI,所支持的类型有

在这里插入图片描述

为避免每次对RemoteViews的操作都通过Binder传输,提供了Action封装对View的操作,如下

在这里插入图片描述

如对于setTextViewText()方法,传入对应操作的方法名

public void setTextViewText(int viewId, CharSequence text) {setCharSequence(viewId, "setText", text);
}

而在setCharSequence()中添加子类ReflectionAction

public void setCharSequence(int viewId, String methodName, CharSequence value) {addAction(new ReflectionAction(viewId, methodName, ReflectionAction.CHAR_SEQUENCE, value));
}

并将Action添加到ArrayList

private void addAction(Action a) {......if (mActions == null) {mActions = new ArrayList<>();}mActions.add(a);
}

每当调用setxxx()方法时,并不会立即更新界面,而必须要通过NotificationManager的notify()或AppWidgetManager的updateAppWidget(),其内部会调用RemoteViews的

  • apply():加载布局并更新界面
  • reApply():只会更新界面

如下为AppWidgetHostView的updateAppWidget()方法

public void updateAppWidget(RemoteViews remoteViews) {applyRemoteViews(remoteViews, true);
}protected void applyRemoteViews(RemoteViews remoteViews, boolean useAsyncIfPossible) {......if (content == null && layoutId == mLayoutId) {try {remoteViews.reapply(mContext, mView, mOnClickHandler);content = mView;recycled = true;if (LOGD) Log.d(TAG, "was able to recycle existing layout");} catch (RuntimeException e) {exception = e;}}if (content == null) {try {content = remoteViews.apply(mContext, this, mOnClickHandler);if (LOGD) Log.d(TAG, "had to inflate new layout");} catch (RuntimeException e) {exception = e;}}......
}

apply()方法通过inflateView()获取View返回

public View apply(Context context, ViewGroup parent, OnClickHandler handler) {RemoteViews rvToApply = getRemoteViewsToApply(context);View result = inflateView(context, rvToApply, parent);rvToApply.performApply(result, parent, handler);return result;
}

performApply()则是遍历调用Action的apply()方法

private void performApply(View v, ViewGroup parent, OnClickHandler handler) {if (mActions != null) {handler = handler == null ? DEFAULT_ON_CLICK_HANDLER : handler;final int count = mActions.size();for (int i = 0; i < count; i++) {Action a = mActions.get(i);a.apply(v, parent, handler);}}
}

再看子类ReflectionAction中apply()具体实现,可知其通过反射调用

@Override
public void apply(View root, ViewGroup rootParent, OnClickHandler handler) {final View view = root.findViewById(viewId);if (view == null) return;Class<?> param = getParameterType();if (param == null) {throw new ActionException("bad type: " + this.type);}try {getMethod(view, this.methodName, param, false /* async */).invoke(view, this.value);} catch (Throwable ex) {throw new ActionException(ex);}
}

模拟RemoteViews

如下模拟在MainActivity中通过广播传递RemoteViews,修改SecondActivity中的布局,manifest如下

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"package="com.demo.demo0"><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/AppTheme"><activityandroid:name=".SecondActivity"android:process=":remote"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activity android:name=".MainActivity"></activity></application></manifest>

MainActivity创建RemoteViews并发送广播

public class MainActivity extends AppCompatActivity {private static final String TAG = "MainActivity";protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification);remoteViews.setTextViewText(R.id.tv, "Hello RemoteViews");remoteViews.setImageViewResource(R.id.iv, R.drawable.ic_launcher);Intent remoteViewsIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, remoteViewsIntent, PendingIntent.FLAG_UPDATE_CURRENT);remoteViews.setOnClickPendingIntent(R.id.root, pendingIntent);Intent broadcastIntent = new Intent(SecondActivity.ACTION_REMOTE_VIEWS);broadcastIntent.putExtra(SecondActivity.EXTRA_REMOTE_VIEWS, remoteViews);sendBroadcast(broadcastIntent);finish();}
}

布局notification.xml如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/root"android:layout_width="match_parent"android:layout_height="match_parent"><ImageViewandroid:id="@+id/iv"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1" /><TextViewandroid:id="@+id/tv"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center_vertical" />
</LinearLayout>

SecondActivity接收广播获取RemoteViews,调用apply方法并把View添加到自身布局

public class SecondActivity extends AppCompatActivity {private static final String TAG = "SecondActivity";private LinearLayout mRemoteViesContainer;public static final String ACTION_REMOTE_VIEWS = "ACTION_REMOTE_VIEWS";public static final String EXTRA_REMOTE_VIEWS = "EXTRA_REMOTE_VIEWS";private BroadcastReceiver mRemoteViewsReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {RemoteViews remoteViews = intent.getParcelableExtra(EXTRA_REMOTE_VIEWS);if (remoteViews != null) {updateUI(remoteViews);}}};private void updateUI(RemoteViews remoteViews) {View view = remoteViews.apply(getApplicationContext(), mRemoteViesContainer);mRemoteViesContainer.addView(view);}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_second);initView();startActivity(new Intent(this, MainActivity.class));}private void initView() {mRemoteViesContainer = findViewById(R.id.remote_views_container);IntentFilter intentFilter = new IntentFilter(ACTION_REMOTE_VIEWS);registerReceiver(mRemoteViewsReceiver, intentFilter);}@Overrideprotected void onDestroy() {super.onDestroy();unregisterReceiver(mRemoteViewsReceiver);}
}

SecondActivity布局为一个空的LinearLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/remote_views_container"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"></LinearLayout>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/29109.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【Linux取经路】进程的奥秘

文章目录 1、什么是进程&#xff1f;1.1 自己写一个进程 2、操作系统如何管理进程&#xff1f;2.1 描述进程-PCB2.2 组织进程2.3 深入理解进程 3、Linux环境下的进程3.1 task_struct3.2 task_struct内容分类3.3 组织进程3.4 查看进程属性 4、结语 1、什么是进程&#xff1f; 在…

软件单元测试

单元测试目的和意义 对于非正式的软件&#xff08;其特点是功能比较少&#xff0c;后续也不有新特性加入&#xff0c;不用负责维护&#xff09;&#xff0c;我们可以使用debug单步执行&#xff0c;内存修改&#xff0c;检查对应的观测点是否符合要求来进行单元测试&#xff0c…

把网站改为HTTPS访问方法

HTTPS是使用TSL/SSL加密超文本传输协议的扩展&#xff0c;用于跨网络的安全传输。网站更改为HTTPS&#xff0c;直接在网站形象上可以得到提升&#xff0c;更重要的是您的网站肯定会在排名和提升方面受益。机密信息的交换需要受到保护&#xff0c;以阻止未经授权的访问。 加密&a…

类加载机制——双亲委派机制

类加载器分类 类加载器 类加载器&#xff08;英文&#xff1a;ClassLoader&#xff09;负责加载 .class 字节码文件&#xff0c;.class 字节码文件在文件开头有特定的文件标识。ClassLoader 只负责 .class 字节码文件的加载&#xff0c;至于它是否可以运行&#xff0c;则由 E…

Vue-组件二次封装

本次对el-input进行简单封装进行演示 封装很简单&#xff0c;就给激活样式的边框(主要是功能) 本次封装主要使用到vue自带的几个对象 $attrs&#xff1a;获取绑定在组件上的所有属性$listeners: 获取绑定在组件上的所有函数方法$slots&#xff1a; 获取应用在组件内的所有插槽 …

成功解决Android设备adb连接后显示device unauthorized

一、提出问题 在电脑通过USB连接新的Android设备&#xff0c;想要通过adb来进行一些操作时&#xff0c;却发现命令提示符上在输入下面命令后显示设备未授权的信息也就是"unauthorized" adb devices二、不可行的解决方案 有人提出的解决方案是打开Android设备的开发…

2023年新手如何学剪辑视频 想学视频剪辑如何入门

随着短视频、vlog等媒体形式的兴起&#xff0c;视频剪辑已经成为了热门技能。甚至有人说&#xff0c;不会修图可以&#xff0c;但不能不会剪视频。实际上&#xff0c;随着各种智能软件的发展&#xff0c;视频剪辑已经变得越来越简单。接下来&#xff0c;一起来看看新手如何学剪…

【ChatGPT 指令大全】怎么使用ChatGPT来帮我们写作

在数字化时代&#xff0c;人工智能为我们的生活带来了无数便利和创新。在写作领域&#xff0c;ChatGPT作为一种智能助手&#xff0c;为我们提供了强大的帮助。不论是作文、文章&#xff0c;还是日常函电&#xff0c;ChatGPT都能成为我们的得力助手&#xff0c;快速提供准确的文…

MySQL — MVCC

文章目录 MVCCMVCC 实现原理隐藏字段undo logundo log的用途undo log类型 版本链ReadView MVCC InnoDB是一个多版本的存储引擎。它保留有关已更改行的旧版本的信息&#xff0c;以支持并发和回滚等事务性特性。这些信息存储在undo表空间中的数据结构称为回滚段。InnoDB使用回滚…

培训报名小程序报名功能完善

目录 1 修改数据源2 修改表单3 支付成功时修改状态4 创建报名成功页5 最终的效果总结 目前我们的报名功能已经搭建了一个基础版&#xff0c;后续需要展示用户已经报名的信息&#xff0c;需要添加一个状态来显示用户是否成功付费。 1 修改数据源 打开我们的报名数据源&#xff…

【基础类】—前端算法类

一、排序 1. 排序方法列表 2. 常见排序方法 快速排序选择排序希尔排序 二、堆栈、队列、链表 堆栈、队列、链表 三、递归 递归 四、波兰式和逆波兰式 理论源码

机器学习笔记之优化算法(十一)梯度下降法:凸函数VS强凸函数

机器学习笔记之优化算法——梯度下降法&#xff1a;凸函数VS强凸函数 引言凸函数&#xff1a;凸函数的定义与判定条件凸函数的一阶条件凸函数的梯度单调性凸函数的二阶条件 强凸函数强凸函数的定义强凸函数的判定条件强凸函数的一阶条件强凸函数的梯度单调性强突函数的二阶条件…

前后端分离式项目架构流程复盘之宿舍管理系统

文章目录 &#x1f412;个人主页&#x1f3c5;JavaEE系列专栏&#x1f4d6;前言&#xff1a;【&#x1f387;前端】先创建Vue-cli项目&#xff08;版本2.6.10&#xff0c;仅包含babel&#xff09;&#xff0c;请选择此项目并创建 【整理简化项目模板】【&#x1f380;创建路由】…

git clean 命令

git clean -n //显示要删除的文件&#xff0c;clean的演习&#xff0c;告诉哪些文件删除&#xff0c;只是一个提醒。 git clean -dn //显示要删除的文件和目录 git clean -f //删除未追踪的文件 git clean -dff //删除未追踪的目录 git clean -df //清除所有未跟踪文件&#xf…

Netty面试题1

计算机网络模型 OSI采用了分层的结构化技术&#xff0c;共分七层&#xff0c; 物理层、数据链路层、网络层、传输层、会话层、表示层、应用层 。 Open System Interconnect 简称OSI&#xff0c;是国际标准化组织(ISO)和国际电报电话咨询委员会(CCITT)联合制定的开放系统互连参…

互联网医院|线上医疗平台连接医者和患者的桥梁

近年来&#xff0c;随着互联网技术的飞速发展&#xff0c;互联网医院系统悄然崛起&#xff0c;引领着医疗行业的变革浪潮。这一系统以其出色的功能与服务&#xff0c;为广大患者带来了便捷、高效的医疗体验&#xff0c;将传统医疗模式推向了新的高度。 作为医疗界的新生力量&a…

FFmpeg常见命令行(三):FFmpeg转码

前言 在Android音视频开发中&#xff0c;网上知识点过于零碎&#xff0c;自学起来难度非常大&#xff0c;不过音视频大牛Jhuster提出了《Android 音视频从入门到提高 - 任务列表》。本文是Android音视频任务列表的其中一个&#xff0c; 对应的要学习的内容是&#xff1a;如何使…

七月学习总结

一晃暑期七月份已经结束了&#xff0c;八月份需要做的事情更多。 在成长的路上不断地迷茫&#xff0c;不断地前进。到底才能完成对自己地救赎。 目前想的就是以后走软件开发&#xff0c;往架构方向做&#xff0c;主语言Java或者go&#xff0c;408基础一定要扎实&#xff0c;计…

新型网络安全:从过程到明确结果

内容 过去的情况网络安全是理论性的&#xff0c;结果才是实际性的。这可能吗&#xff1f;我们现在的努力方向结论 本文讲述了为什么企业必须重新思考其网络安全方法&#xff1a;旧方法是否足够有效&#xff0c;是否可以完全适用&#xff1f;公司应采取哪些行动来实现内部信息…

【LeetCode】【数据结构】单链表OJ常见题型(二)

&#x1f440;樊梓慕&#xff1a;个人主页 &#x1f3a5;个人专栏&#xff1a;《C语言》《数据结构》《蓝桥杯试题》《LeetCode刷题笔记》 &#x1f31d;每一个不曾起舞的日子&#xff0c;都是对生命的辜负 目录 前言&#xff1a; 【LeetCode】面试题02.04. 分割链表 【Lee…