Android学习--深入探索RemoteViews

什么是RemoteViews

RemoteViews表示的是一个View结构,它可以在其他进程中显示,由于它在其他进程中显示,为了能够及时更新它的界面,RemoteViews提供了一组基础的操作来跨进程更新它的界面。源码中对于它的解释如下:

/**
 * A class that describes a view hierarchy that can be displayed in
 * another process. The hierarchy is inflated from a layout resource
 * file, and this class provides some basic operations for modifying
 * the content of the inflated hierarchy.
 */

RemoteViews相信很多人跟我一样觉得这可能是一个View或是layout。真的是这样吗?其实不然上面描述中说到它是描述一个View结构,并不是一个View,下面我们来通过源码看看RemoteViews

public class RemoteViews implements Parcelable, Filter {
   
   
......
}

从它的继承方式来看,它跟View和Layout并没有什么关系。下面我们来看看RemoteViews如何使用。

RemoteViews的应用场景

1、应用于通知栏

2、应用于桌面小部件

RemoteViews的使用

前面说了RemoteViews用于通知栏和桌面小部件,下面我们一个个来看RemoteViews是怎么使用的。

RemoteViews在通知栏中使用

RemoteViews在通知栏中的应用还是比较简单的,话不多说我们直接撸代码

    Notification notification = new Notification();
    notification.icon = R.mipmap.ic_launcher;
    notification.tickerText = "hello notification";
    notification.when = System.currentTimeMillis();
    notification.flags = Notification.FLAG_AUTO_CANCEL;
    Intent intent = new Intent(this, RemoteViewsActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.layout_notification);//RemoveViews所加载的布局文件
    remoteViews.setTextViewText(R.id.tv, "RemoteViews应用于通知栏");//设置文本内容
    remoteViews.setTextColor(R.id.tv, Color.parseColor("#abcdef"));//设置文本颜色
    remoteViews.setImageViewResource(R.id.iv, R.mipmap.ic_launcher);//设置图片
    PendingIntent openActivity2Pending = PendingIntent.getActivity
            (this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);//设置RemoveViews点击后启动界面
    remoteViews.setOnClickPendingIntent(R.id.tv, openActivity2Pending);
    notification.contentView = remoteViews;
    notification.contentIntent = pendingIntent;
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(2, notification);

RemoteViews在桌面小部件中的应用

桌面小部件是通过AppWidgetProVider来实现的,而AppWidgetProVider继承自BroadcastReceiver,所以可以说AppWidgetProVider是个广播。

1、定义小部件的界面

首先,我们需要在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">

    <ImageView
        android:id="@+id/iv"
        android:layout_width="360dp"
        android:layout_height=
----《深入探索Android热修复技术原理》高清完整版PDF---- 2017年6月,阿里巴巴手淘技术团队推出了史上首个非侵入式移动热更新解决方案——Sophix。在Android热修复的三大领域:代码修复、资源修复、SO修复方面,以及方案的安全性和易用性方面,Sophix都做到了业界领先。 《深入探索Android热修复技术原理》从阿里Sophix方案开发过程入手权威解读,分享了阿里巴巴手淘技术团队对系统底层的原创性发现,是业界首部全方位完整介绍热修复原理的书籍。 阿里技术大牛联袂推荐 自 2014 年至今,手淘定义和引领了业界 Android 组件化和热修复技术风潮,至于后来者 Instant App 或多或少也受了国内技术风气影响。今天看到团队同学将这块技术认真系统化整理成书,非常欣喜。在这本书里,既能看到对热修复技术风潮的发展历史系统深入总结,看到国内程序员在Android系统级技术持续突破上的不懈努力,更看到国内程序员坚持打造世界级优秀专业移动技术产品的雄心壮志! 手机淘宝基础平台部负责人,阿里巴巴资深技术专家 吴天华(天施) 业内少有的讲解 Android 热修复的深度书籍,对于原理、代码讲解得非常的清晰和深入,值得 Android 工程师研读。 手机淘资深专家,倪生华(玄黎) 应用热修复是一项略带神秘而又颇具争议的技术,但是它的确赋予应用开发者“驾着飞机修引擎”的能力。本书从 Android 应用热修复技术的原理及代码实现、多种方案进行比较的角度,系统化地阐述了 Android 平台上的应用热修复技术。对 Android 应用热修复有好奇心的技术人员,这本专题书不容错过。 计算机技术领域著名作家,阿里巴巴飞猪事业部首席架构师 潘爱民
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

紫雾凌寒

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值