Android-通知栏上的RemoteView

Android-通知栏上的RemoteView

学习自

《Android开发艺术探索》
https://developer.android.google.cn/reference/android/widget/RemoteViews

RemoteView漫谈

什么是RemoteView?先找官方文档

以下内容来自于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.
该类描述了一个能够被显示在其他进程中的View。该View的层次结构来自资源文件,并且此类提供了一下基础的操作来帮助修改View的内容。

从上面的官方文档中我们了解到,远程View之所以被称作为远程View,是因为他们运行在另一个进程中当然远了? 。 RemoteView在开发中主要会作用于两个方面 通知栏 桌面小部件 。因为View是存在于另一个进程中的所以我们不能够像直接操作View的那样来操作RemoteView,为了帮助我们操作RemoteView呢,Google为我们提供了 RemoteViews 类来帮助我们操作RemoteView。

这篇文章先来学习通知栏上的RemoteView,在下面的文章中我们会学习桌面小部件。

Notification与RemoteView

在通知栏上的RemoteView是依托于通知的(-_-||)。通过RemoteView我们可以实现自定义的通知样式。下面是一个系统默认样式的通知。

fun send(view: View) {
    var intent = Intent(this, MainActivity::class.java)
    var pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT)
    var notification = Notification.Builder(this).apply {
        setTicker("Hello world")
        setSmallIcon(R.mipmap.ic_launcher)
        setContentTitle("This is a title")
        setContentText("Content......")
        setLargeIcon(BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher))
        setContentIntent(pendingIntent)
    }.build()
    var manager = this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    manager.notify(1, notification)
}

我想上面的发送通知的代码大家一定很熟悉,但是有有时候系统默认的通知并不能满足我们的需要,往往我们需要自定义通知的样式。这时候就需要RemoteView了。

下面是一个RemoteView模仿通知栏播放音乐的控制器的Demo,在实际的开发中还需要用到前台任务,这里就是简单模仿一下当不得真。

image.png | left | 373x95

发送通知的代码。

fun send2(view: View) {
    var intent = Intent(this, MainActivity::class.java)
    var pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)

    var builder = Notification.Builder(this)
    builder.setSmallIcon(R.mipmap.ic_launcher)
    builder.setAutoCancel(false)

    var remoteViews = RemoteViews(packageName, R.layout.music_remote)
    remoteViews.setTextViewText(R.id.singNameTV, "牵丝戏")
    //设置View的点击事件,视具体情况可以开启Activity/发送通知/开启服务等
    //这里直接模拟一下,无所谓什么了
    remoteViews.setOnClickPendingIntent(R.id.previousIV, pendingIntent)
    remoteViews.setOnClickPendingIntent(R.id.pauseIV, pendingIntent)
    remoteViews.setOnClickPendingIntent(R.id.nextIV, pendingIntent)

    builder.setCustomContentView(remoteViews)

    var manager = this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    manager.notify(1, builder.build())
}

总结

通知栏的RemoteView的内荣比较少也比较简单就没做过多的介绍,通知栏的RemoteView的用途相当广泛: 播放音乐,安全类的软件的快速操作等。在下一章的内容呢,将会学习另一种RemoteView---桌面小部件。

转载于:https://www.cnblogs.com/slyfox/p/9340140.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RemoteViewsAndroid 中的一个特殊类,用于在一个进程中创建布局并将其在另一个进程中显示。它是用于在应用程序之间共享布局和视图的一种机制。 使用 RemoteViews,你可以创建一个布局,并将其传递给另一个进程,以便在该进程中显示。这在 Android 中的一些场景中非常有用,比如创建小部件、通知和锁屏界面。 要使用 RemoteViews,首先需要创建一个布局文件,就像你在普通的视图中所做的那样。然后,使用 RemoteViews 实例化一个对象,并设置要在布局中显示的视图和数据。 下面是一个简单的示例,演示如何使用 RemoteViews 创建一个包含 TextView 的布局,并将其在另一个进程中显示: ```java // 创建 RemoteViews 实例 RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.my_layout); // 设置 TextView 的文本 remoteViews.setTextViewText(R.id.text_view, "Hello World!"); // 使用 AppWidgetManager 更新小部件 AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId, remoteViews); ``` 这是一个基本的示例,你可以根据需要自定义布局和设置其他视图属性。 需要注意的是,RemoteViews 支持的视图类型有限,不支持所有的 Android 视图。你可以在官方文档中查看 RemoteViews 支持的视图类型。 希望这个简单的介绍能够帮助你了解 RemoteViews 的基本用法。如果你有更多的问题,欢迎继续提问!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值