Android 关于RemoteViews的理解(一)

前言

RemoteViews从字面上理解是远程View,这个理解可能有点抽象,我们听过远程服务,但是远程View听说过的Android开发者应该很少,其实远程View和远程Service是一样的。谷歌设计这个View的主要目的是为了跨进程更新界面,基于这个前提我们在Android设备上这用得到RemoteViews的应用场景主要有两个地方:通知栏和桌面小部件,我打算用三篇文章去了解RemoteViews,第一篇介绍RemoteViews的使用场景。第二篇是分析RomoteViews的内部运行机制,第三篇则是分析RomoteViews的意义和跨进程更新界面的场景。


通知栏里的RemoteViews

系统通知栏我们应该很了解,这个是APP促活的一个关键手段,通过定时或活动时弹出Notification让用户点击促进App的用户粘性,同时也可以让用户查看某些功能的状态,但是另一个方面来说这个功能使用很多时候会打扰用户,让用户不堪其扰,当然这是题外话,技术永远是为了业务服务的。回到主题,RemoteViews在通知栏上的应用可以有两种状态,一个是使用系统默认效果,另一个是自定义布局。
使用系统默认布局的代码比较简单,我这里直接列出代码:

   val intent = Intent(this, NotificationOpenActivity::class.java)
            val pendingIntent =
                PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT)
            val channelId= "channelId"
            val channelName = "channelName"
            val channel = NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_LOW)

            var notification = NotificationCompat.Builder(this, channelId)
                .setContentTitle("notification_title")
                .setContentText("notification_content")
                .setWhen(System.currentTimeMillis())
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                .setSmallIcon(R.mipmap.ic_launcher)
                .setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_LIGHTS)
                .setContentIntent(pendingIntent)
                .build()

            val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            manager.createNotificationChannel(channel)
            manager.notify(1, notification)

上述代码就可以弹出一个系统默认样式的通知,点击通知则清除通知,并跳转至NotificationOpenActivity,当发送通知栏的时候APP启动icon右上角也会有数量标注。

Android 关于RemoteViews的理解(一)_第1张图片

在实际项目开发中我们往往需要自定义通知栏布局,这个也比较简单,我们只需要自定义一个布局文件,然后利用RemoteViews来加载布局文件就可以更改通知栏的样式,自定义通知栏代码如下:

布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/iv_notification"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp" />

    <TextView
        android:id="@+id/tv_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="6dp"
        android:layout_toRightOf="@id/iv_notification" />
</RelativeLayout>

页面逻辑:

  val openActivityPendingIntent = PendingIntent.getActivity(
                this,
                0,
                Intent(this, NotificationOpenActivity::class.java),
                PendingIntent.FLAG_UPDATE_CURRENT
            )
            val channelId = "channelId"
            val channelName = "channelName"
            val channel =
                NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_LOW)

            val remoteViews = RemoteViews(packageName, R.layout.view_notification)
            remoteViews.setTextViewText(R.id.tv_content, "自定义通知栏文字")
            remoteViews.setImageViewResource(R.id.iv_notification, R.drawable.icon_notification)
            remoteViews.setOnClickPendingIntent(R.id.btn_custom_notificateion, openActivityPendingIntent)

            val notification = NotificationCompat.Builder(this, channelId)
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(true)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setDefaults(Notification.DEFAULT_LIGHTS)
                .setContentIntent(openActivityPendingIntent)
                .setCustomContentView(remoteViews)
                .build()
            
            val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            manager.createNotificationChannel(channel)
            manager.notify(1, notification)

效果如下:
Android 关于RemoteViews的理解(一)_第2张图片
以上就是RemoteViews在通知栏的使用,使用起来相对简单,只要提供当前应用的包名和布局文件的资源id就可以构建一个RemoteViews对象,通过布局内部子id

  remoteViews.setTextViewText(R.id.tv_content, "自定义通知栏文字")
  remoteViews.setImageViewResource(R.id.iv_notification, R.drawable.icon_notification)

就可以对文字和图片进行设置,然后RemoteViews点击事件方法则为:

remoteViews.setOnClickPendingIntent(R.id.btn_custom_notificateion, openActivityPendingIntent)

这里更新RemoteViews或许大家会觉得复杂,为什么RemoteViews没有提供类似View的findViewById这个方法呢,提供这个方法不就能获取RemoteViews里 的子View了,操作岂不是更简单,RomoteViews的内部运行机制会在第二篇文章中进行了解,大家可以留个意。


桌面小部件上的RemoteViews

桌面小部件在Android开发里的实现类是AppWidgetProvider,它本质上来说是一个广播(BroadcastReceiver),因为这个类继承的就是BroadcastReceiver。要了解RemoteViews在桌面小部件的应用我们需要先了解桌面小部件的使用,下面我们一步步做一个桌面小部件。

1:界面布局如下:

<?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_app_widget"
        android:layout_width="wrap_content"
        android:src="@drawable/icon_notification"
        android:layout_height="wrap_content" />
</LinearLayout>

这个layout布局没什么好说的,Android开发者都知道。

2:新建xml文件夹并新建一个桌面小部件配置信息文件,我这里这个配置信息文件命名为:app-widget_provider_info.xml

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

</appwidget-provider>

这里的配置文件含义也比较明显,分别是初始化布局和小部件最小宽高,值得说的是

  android:updatePeriodMillis="10000"

这个参数的含义是定义小部件自动更新的周期,单位是毫秒,每个周期之后都会触发小部件的自动更新。

3:定义小部件的广播接收者

代码如下:

package com.sjr.remoteviewsdemo

import 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.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Matrix
import android.os.SystemClock
import android.util.Log
import android.widget.RemoteViews
import android.widget.Toast

/**
 * Created by sjr on 2020/5/1
 */
class MyAppWidgetProvider : AppWidgetProvider() {

    override fun onReceive(context: Context, intent: Intent) {
        super.onReceive(context, intent)
        
        if (intent.action == CLICK_ACTION) {
            Toast.makeText(context, "clicked it", Toast.LENGTH_SHORT).show()

            Thread(Runnable {
                val srcbBitmap = BitmapFactory.decodeResource(
                    context.resources, R.drawable.icon_notification
                )
                val appWidgetManager = AppWidgetManager.getInstance(context)
                for (i in 0..36) {
                    val degree = (i * 10 % 360).toFloat()
                    val remoteViews = RemoteViews(
                        context
                            .packageName, R.layout.view_app_widget
                    )
                    remoteViews.setImageViewBitmap(
                        R.id.iv_app_widget,
                        rotateBitmap(srcbBitmap, degree)
                    )
                    val intentClick = Intent()
                    intentClick.action = CLICK_ACTION
                    val pendingIntent = PendingIntent
                        .getBroadcast(context, 0, intentClick, 0)
                    remoteViews.setOnClickPendingIntent(R.id.iv_app_widget, pendingIntent)
                    appWidgetManager.updateAppWidget(
                        ComponentName(
                            context, MyAppWidgetProvider::class.java
                        ), remoteViews
                    )
                    SystemClock.sleep(30)
                }
            }).start()
        }
    }


    override fun onUpdate(
        context: Context, appWidgetManager: AppWidgetManager,
        appWidgetIds: IntArray
    ) {
        super.onUpdate(context, appWidgetManager, appWidgetIds)

        val counter = appWidgetIds.size

        for (i in 0 until counter) {
            val appWidgetId = appWidgetIds[i]
            onWidgetUpdate(context, appWidgetManager, appWidgetId)
        }

    }
    
    
    private fun onWidgetUpdate(
        context: Context,
        appWidgeManger: AppWidgetManager, appWidgetId: Int
    ) {


        val remoteViews = RemoteViews(
            context.packageName,
            R.layout.view_app_widget
        )

        // 窗口小部件点击事件发送的Intent广播
        val intentClick = Intent()
        intentClick.action = CLICK_ACTION
        val pendingIntent = PendingIntent.getBroadcast(
            context, 0,
            intentClick, 0
        )
        remoteViews.setOnClickPendingIntent(R.id.iv_app_widget, pendingIntent)
        appWidgeManger.updateAppWidget(appWidgetId, remoteViews)
    }

    private fun rotateBitmap(srcbBitmap: Bitmap, degree: Float): Bitmap {
        val matrix = Matrix()
        matrix.reset()
        matrix.setRotate(degree)
        return Bitmap.createBitmap(
            srcbBitmap, 0, 0,
            srcbBitmap.width, srcbBitmap.height, matrix, true
        )
    }

    companion object {

        val CLICK_ACTION = "com.sjr.remoteviewsdemo.action.CLICK"
    }
}

以上代码就实现了一个简单的桌面小部件,小部件显示一张图片,将下够不见添加至桌面之后,点击小部件小部件会旋转一周,可以看到小部件的布局更新是通过RemoteViews来实现的。

4:在AndroidManifest.xml中声明小部件


        <receiver android:name=".MyAppWidgetProvider" >
            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/app_widget_provider_info" >
            </meta-data>

            <intent-filter>
                <action android:name="com.sjr.remoteviewsdemo.action.CLICK" />
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
        </receiver>

四大组件都需要在AndroidManifest中声明,桌面小部件本质上也是一个BroadcastReceiver,所以也需要在此进行注册,上面的小部件一共有两个action,一个是用来识别小部件的点击,一个是系统规定作为小部件必须有的标识。

AppWidgetProvider的onREceive方法的分发源码如下:

/**
     * Implements {@link BroadcastReceiver#onReceive} to dispatch calls to the various
     * other methods on AppWidgetProvider.  
     *
     * @param context The Context in which the receiver is running.
     * @param intent The Intent being received.
     */
    // BEGIN_INCLUDE(onReceive)
    public void onReceive(Context context, Intent intent) {
        // Protect against rogue update broadcasts (not really a security issue,
        // just filter bad broacasts out so subclasses are less likely to crash).
        String action = intent.getAction();
        if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) {
            Bundle extras = intent.getExtras();
            if (extras != null) {
                int[] appWidgetIds = extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);
                if (appWidgetIds != null && appWidgetIds.length > 0) {
                    this.onUpdate(context, AppWidgetManager.getInstance(context), appWidgetIds);
                }
            }
        } else if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
            Bundle extras = intent.getExtras();
            if (extras != null && extras.containsKey(AppWidgetManager.EXTRA_APPWIDGET_ID)) {
                final int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
                this.onDeleted(context, new int[] { appWidgetId });
            }
        } else if (AppWidgetManager.ACTION_APPWIDGET_OPTIONS_CHANGED.equals(action)) {
            Bundle extras = intent.getExtras();
            if (extras != null && extras.containsKey(AppWidgetManager.EXTRA_APPWIDGET_ID)
                    && extras.containsKey(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS)) {
                int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
                Bundle widgetExtras = extras.getBundle(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS);
                this.onAppWidgetOptionsChanged(context, AppWidgetManager.getInstance(context),
                        appWidgetId, widgetExtras);
            }
        } else if (AppWidgetManager.ACTION_APPWIDGET_ENABLED.equals(action)) {
            this.onEnabled(context);
        } else if (AppWidgetManager.ACTION_APPWIDGET_DISABLED.equals(action)) {
            this.onDisabled(context);
        } else if (AppWidgetManager.ACTION_APPWIDGET_RESTORED.equals(action)) {
            Bundle extras = intent.getExtras();
            if (extras != null) {
                int[] oldIds = extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_OLD_IDS);
                int[] newIds = extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);
                if (oldIds != null && oldIds.length > 0) {
                    this.onRestored(context, oldIds, newIds);
                    this.onUpdate(context, AppWidgetManager.getInstance(context), newIds);
                }
            }
        }
    }

由以上源码可以看到,根据不同的Action,OnReceive会调用onEnable、onDisable、onUpdate
下面逐个说明:

  • onEnable:当小部件第一次添加到桌面的时候会调用这个方法,添加到桌面可以多次,但是这个方法只会被调用一次;
  • onUpdate:小部件被点击或者周期间隔会调用这个方法;
  • onDelete :每删除一次桌面小部件就会调用一次;
  • onDisabled:当最后一个该类型的小部件被删除时会调用这个方法;
  • onREceive:分发具体的事件给各个方法;

到这里就是整个桌面小部件开发的流程了,从整个流程我们可以发现,桌面小部件的操作都是通过RemoteViews来完成的,不管是初始化还是更新。


小结

通过这两个RemoteViews的应用我们初步了解了RemoteViews这个远程View,由于篇幅有限,所以不会在一篇内了解完RemoteViews,下一篇文章会分析RemoteViews的机制。

你可能感兴趣的:(Android)