RemoteView(kotlin)

使用场景:通知栏&桌面部件

自定义通知栏

  1. 通知权限申请
    manifest配置
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

权限动态申请

package com.example.kotlinlearn.Common;import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.Settings;
import android.widget.Toast;import androidx.activity.ComponentActivity;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.core.content.ContextCompat;import java.util.ArrayList;
import java.util.List;
import java.util.Map;public class PermissionUtils {private static PermissionUtils permissionUtils;private String[] permissions = {Manifest.permission.POST_NOTIFICATIONS};private List<String> permissionList = new ArrayList<>();private ActivityResultLauncher<String[]> permissionLauncher;public static synchronized PermissionUtils getInstance() {if (permissionUtils == null) {permissionUtils = new PermissionUtils();}return permissionUtils;}private PermissionUtils() {}public void checkPermission(ComponentActivity activity) {permissionList.clear(); // Clear previous permission requests// Initialize the launcher if not already initializedif (permissionLauncher == null) {initLaunchers(activity);}for (String permission : permissions) {if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {permissionList.add(permission);}}permissionLauncher.launch(permissionList.toArray(new String[0]));}private void initLaunchers(ComponentActivity activity) {// Initialize the launcher for requesting permissionspermissionLauncher = activity.registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(),new ActivityResultCallback<Map<String, Boolean>>() {@Overridepublic void onActivityResult(Map<String, Boolean> result) {}});}
}
  1. 实现通知
package com.example.kotlinlearn.RemoteViewimport android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.widget.RemoteViews
import androidx.core.app.NotificationCompat
import com.example.kotlinlearn.Robject NotificationUtil {private const val CHANNEL_ID = "my_channel_id"private const val CHANNEL_NAME = "My Channel"fun showCustomNotification(context: Context) {// 创建通知渠道(仅适用于 Android O 及以上版本)if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {val channel = NotificationChannel(CHANNEL_ID, CHANNEL_NAME,NotificationManager.IMPORTANCE_DEFAULT).apply {enableLights(true)lightColor = Color.REDenableVibration(true)}val notificationManager = context.getSystemService(NotificationManager::class.java)notificationManager.createNotificationChannel(channel)}// 创建 RemoteViewval remoteViews = RemoteViews(context.packageName, R.layout.notification_layout).apply {setTextViewText(R.id.notification_title, "自定义通知标题")setTextViewText(R.id.notification_content, "这是自定义通知内容")}// 设置点击通知的行为val intent = Intent(context, RemoteViewActivity::class.java)val pendingIntent = PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)val notification = NotificationCompat.Builder(context, CHANNEL_ID).setSmallIcon(R.drawable.ic_launcher_foreground).setCustomContentView(remoteViews).setContentIntent(pendingIntent).build()val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManagermanager.notify(1, notification)}
}
  1. activity中申请权限后直接调用就行
package com.example.kotlinlearn.RemoteViewimport android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import com.example.kotlinlearn.Common.PermissionUtils
import com.example.kotlinlearn.Rclass RemoteViewActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)PermissionUtils.getInstance().checkPermission(this)setContentView(R.layout.activity_remote_view)var button = findViewById<Button>(R.id.button)button.setOnClickListener {NotificationUtil.showCustomNotification(this);}}
}

效果
在这里插入图片描述

自定义桌面小组件

  1. 定义组件的布局样式widget_layout.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"android:orientation="vertical"android:padding="16dp"><TextViewandroid:id="@+id/widget_text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello Widget"android:textSize="18sp" /><Buttonandroid:id="@+id/widget_button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Update" />
</LinearLayout>
  1. MyWidgetProvider,需要继承自AppWidgetProvider
package com.example.kotlinlearn.RemoteViewimport android.app.PendingIntent
import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.widget.RemoteViews
import com.example.kotlinlearn.R
import java.util.Randomclass MyWidgetProvider : AppWidgetProvider() {override fun onUpdate(context: Context,appWidgetManager: AppWidgetManager,appWidgetIds: IntArray) {for (appWidgetId in appWidgetIds) {val views = RemoteViews(context.packageName, R.layout.widget_layout)val intent = Intent(context, MyWidgetProvider::class.java)intent.setAction(BUTTON_CLICKED)val pendingIntent = PendingIntent.getBroadcast(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)views.setOnClickPendingIntent(R.id.widget_button, pendingIntent)appWidgetManager.updateAppWidget(appWidgetId, views)}}override fun onReceive(context: Context, intent: Intent) {super.onReceive(context, intent)if (BUTTON_CLICKED == intent.action) {val appWidgetManager = AppWidgetManager.getInstance(context)val views = RemoteViews(context.packageName, R.layout.widget_layout)views.setTextViewText(R.id.widget_text, "Updated!" + Random().nextInt())val componentName = ComponentName(context, MyWidgetProvider::class.java)appWidgetManager.updateAppWidget(componentName, views)}}companion object {private const val BUTTON_CLICKED = "com.example.BUTTON_CLICKED"}
}
  1. 在res/xml下创建组件的属性文件my_widget_info.xml,包括大小等值
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"android:minWidth="125dp"android:minHeight="50dp"android:updatePeriodMillis="86400000"android:initialLayout="@layout/widget_layout" />
  1. 在manifest中配置receiver,与activity同级
        <receiver android:name=".RemoteView.MyWidgetProvider" android:exported="true"><intent-filter><action android:name="android.appwidget.action.APPWIDGET_UPDATE" /></intent-filter><meta-dataandroid:name="android.appwidget.provider"android:resource="@xml/my_widget_info" /></receiver>

效果
在这里插入图片描述
在这里插入图片描述
点击后text会显示随机的数字。

原理

  1. 可以很简单的看到,RemoteViews实现了Parcelable,所以是可序列化的,可以在进程之间传递。
    在这里插入图片描述
  2. 在官网可以看到,RemoteViews只支持基础的view,不支持自定义view,支持的布局以及组件如下所示
    在这里插入图片描述
  3. 从上面的代码示例中可以知道,在更改组件属性时使用的是setTextViewText,而不是findById。
    在这里插入图片描述
    从以上的调用链可以知道,view的设置被封装在反射对象,存在mActions中,是在接收者进行真正的设置。
    可以在Action的子类中找到getMethod(view, this.methodName, param, false /* async */).invoke(view, value);,接收者正是通过反射的方式调用action中封装的view设置方法。

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

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

【笔记】Day2.4表设计说明

主键ID一般使用bigint类型 运送类型 使用比int更小的tinyint类型 eg&#xff1a;普快代表1 特快代表2&#xff08;没写反&#xff09; 关联城市 varchar 2代表京津冀 3代表江浙沪 4代表川渝 首重和续重都有小数点 故使用double 轻抛系数都为整数 故使用int 创建时间和修改…

计算机毕业设计 基于Django的在线考试系统的设计与实现 Python+Django+Vue 前后端分离 附源码 讲解 文档

&#x1f34a;作者&#xff1a;计算机编程-吉哥 &#x1f34a;简介&#xff1a;专业从事JavaWeb程序开发&#xff0c;微信小程序开发&#xff0c;定制化项目、 源码、代码讲解、文档撰写、ppt制作。做自己喜欢的事&#xff0c;生活就是快乐的。 &#x1f34a;心愿&#xff1a;点…

自然语言到 SQL 的曙光:我们准备好了吗?

发布于&#xff1a;2024 年 10 月 08 日 各位读者&#xff0c;国庆假期已过&#xff0c;我们打工人要开启奋斗新征程了&#xff0c;今天小编也是刚上班假期综合征还没过去&#xff0c;就被抓过来读论文&#xff0c;还好我在假期没闲着&#xff0c;整理了几篇关于 NL2SQL 的最新…

Spring与Spring Boot之间的区别

Spring和Spring Boot是用于开发Java企业应用的两个主流框架。虽然它们都属于Spring生态系统的一部分&#xff0c;但是它们各自有不同的使用场景和特点。 在本文中&#xff0c;我们将探讨Spring与Spring Boot之间的差异&#xff0c;针对他们之间特性的差异&#xff0c;做一个详…

李沐 X 动手学深度学习 深度学习介绍 学习笔记

x轴是不同的模式&#xff1a;符号学---概率模型---机器学习y轴是我们想做的东西&#xff08;问题领域&#xff09;&#xff1a;感知&#xff08;了解这是什么东西&#xff0c;能看见这个物体&#xff09;---&#xff08;做&#xff09;推理&#xff08;基于我看到的东西想象未来…

dvwa:暴力破解、命令注入、csrf全难度详解

暴力破解 easy模式 hydra -L /usr/share/wordlists/SecLists-master/Usernames/top-usernames-shortlist.txt -P /usr/share/wordlists/SecLists-master/Passwords/500-worst-passwords.txt 192.168.72.1 http-get-form "/dvwa/vulnerabilities/brute/:username^USER^&…

RED HAT断电重启报:“Failed to open \EFI\redhat\ grubx64.efi- Not Found“

RED HAT断电重启报错&#xff1a;"Failed to open \EFI\redhat\ grubx64.efi- Not Found"的解决办法。 问题&#xff1a;服务器断电重启导致&#xff0c;文件丢失无法正常启动操作系统。 解决方案&#xff1a; 1、准备一个Red Hat系统镜像或者启动盘挂载到服务器上&…

【AI学习】Mamba学习(五):《HiPPO: Recurrent Memory with Optimal Polynomial Projections》

SSM之后&#xff0c;就需要接着学习HiPPO了。 《HiPPO: Recurrent Memory with Optimal Polynomial Projections》 论文地址&#xff1a;https://arxiv.org/abs/2008.07669 摘要 从连续数据中学习的一个核心问题是&#xff0c;随着更多数据的处理&#xff0c;以增量方式表示累…

YOLO11训练自己的数据集(吸烟、跌倒行为检测)

YOLO11训练自己的数据集&#xff08;吸烟、跌倒行为检测&#xff09; 前言相关介绍前提条件实验环境安装环境项目地址LinuxWindows 使用YOLO11训练自己的数据集进行吸烟、跌倒行为检测准备数据进行训练进行预测进行验证 参考文献 前言 由于本人水平有限&#xff0c;难免出现错漏…

柯桥外语培训韩语学习考级韩语中TOPIK常用语法表达

-기 위해서는 -는 것이 좋다 为了......&#xff0c;......比较好 -는 것보다는 -는 것이 좋다 比起......&#xff0c;......比较好 -(으)려면 -아/어/야 한다 如果想......的话&#xff0c;得...... -왜냐하면 -기 때문이다 因为...... -그 이유는 -기 때문이다 理由是…

RabbitMQ快速入手

核心概念 界⾯上的导航栏共分6部分,这6部分分别是什么意思呢? 我们先看看RabbitMQ的⼯作流程: RabbitMQ是⼀个消息中间件,也是⼀个⽣产者消费者模型.它负责接收,存储并转发消息. Producer和Consumer Producer: ⽣产者,是RabbitMQServer的客⼾端,向RabbitMQ发送消息 Consume…

PDF处理技巧:Windows电脑如何选择合适的 PDF 编辑器

您可以阅读本文以了解用于在 PC 上编辑 PDF 的顶级免费软件&#xff0c;而无需花费任何费用即可轻松进行快速编辑、拆分、合并、注释、转换和共享您的 PDF。 PDF 或可移植文档文件是由 Adobe 创建的一种多功能文件格式。它可以帮助您轻松可靠地交换文档&#xff0c;无论相关方…

毕业设计 | ESP32-Album 开源 AIoT 智能云相框,支持 Wi-Fi 无线图传,平替小米电子相册...

数码相框主要由三个部分组成: LCD液晶显示屏&#xff0c;ESP32主控板和外框 ESP32-Album 数码相框 ESP32-Album 数码相框是一款基于乐鑫 ESP32-P4 芯片开发的高性能电子相册。凭借ESP32-P4 强大的图像及音视频处理能力&#xff0c;该设备能够实现照片、视频和音乐的播放&#x…

ELK中L的filebeat配置及使用(超详细)

上一次讲解了如何在linux服务器上使用docker配置ELK中的E和K&#xff0c;这期着重讲解一下L怎么配置。 首先L在elk中指的是一个数据处理管道&#xff0c;可以从多种来源收集数据&#xff0c;进行处理和转换&#xff0c;然后将数据发送到 Elasticsearch。L的全称就是&#xff1…

【实践】快速学会使用阿里云消息队列RabbitMQ版

文章目录 1.场景简介2.实验架构3.实验流程3.创建实验资源4.创建阿里云AccessKey5.创建静态用户名密码6.创建Vhost、Exchange、Queue并绑定关系6.1、Vhost 的作用6.2、创建Vhost6.3、Exchange 的作用6.4、创建Exchange6.5、Queue 的作用6.6、创建Queue6.7、创建Exchange和Queue的…

鸿蒙--知乎评论

这里我们将采用组件化的思想进行开发 拆解组件 pages下&#xff0c;新建ZhiHu的文件pages下&#xff0c;新建components, 里面新建 HmNavBar和HmCommentItem components/HmNavBar.ets Entry Component struct HmNavBar {title: string 标题build() {Row() {// 返回键Row() {I…

Kubernetes(K8s)部署

主机名ip角色docker-harbor.revkarl.org172.25.254.250harbor仓库k8s-master172.25.254.100master&#xff0c;k8s集群控制节点k8s-node1172.25.254.10worker&#xff0c;k8s集群工作节点k8s-node2172.25.254.20worker&#xff0c;k8s集群工作节点 注意&#xff1a; 所有节点禁…

毕设分享 基于python的搜索引擎设计与实现

文章目录 0 简介1 课题简介2 系统设计实现2.1 总体设计2.2 搜索关键流程2.3 推荐算法2.4 数据流的实现 3 实现细节3.1 系统架构3.2 爬取大量网页数据3.3 中文分词3.4 相关度排序第1个排名算法&#xff1a;根据单词位置进行评分的函数第2个排名算法&#xff1a;根据单词频度进行…

国内知名人工智能AI大模型专家培训讲师唐兴通讲授AI办公应用人工智能在营销与销售过程中如何应用数字化赋能

AI如火如荼&#xff0c;对商业与社会影响很大。 目前企业广泛应用主要是在营销、销售方向&#xff0c;提升办公效率等方向。 从喧嚣的AI导入营销与销售初步阶段&#xff0c;那么当下&#xff0c;领先的组织与个人现在正在做什么呢&#xff1f; 如何让人性注入冷冰冰的AI&…

Android Studio New里面没有New Flutter Project

跟着Flutter中文网的配置教程&#xff0c;安装好了flutter,在Android studio里面也安装了dart和flutter的插件。重启后还是在FIle->New里面没有显示New Flutter Project。 反复卸载重装dart和flutter插件好几次&#xff0c;依然没有效果。 原来是没有把Android APK Suppor…