RemoteViews
RemoteViews表示的是一个View结构,实现了Parceable接口-它可以在其他进程中显示;使用场景:通知、桌面小部件.
一、RemoteViews的应用
- 通知
- 桌面小部件
通知
Step :
- 指定Intent,通过PendingIntent获取pi
- 获取NotificationManager(Android O及以上需设置Channel)
- 通过Channel创建Manager
- 实例化RemoteViews
- 创建Notifycation(将实例设置进它)并通知
...
private void remoteViewsNotify()
{
// 指定intent
Intent intent = new Intent(this, NotifyActivity.class);
// 相当于startActivity(intent)
PendingIntent pi = PendingIntent.getActivity(this, 0x01, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// 获取NotificationManager
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// 版本大于等于 26 通知需要使用channel
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
NotificationChannel nc = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
nc.setDescription("description");
nc.setLightColor(Color.BLUE);
nc.enableLights(true);
nc.enableVibration(true);
// 偶数表示静止时间,奇数表示振动时间
nc.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
assert manager != null;
manager.createNotificationChannel(nc);
}
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.remoteviews_notify_layout);
remoteViews.setTextViewText(R.id.textView, "新消息");
remoteViews.setImageViewResource(R.id.imageView, R.drawable.webp_2);
remoteViews.setOnClickPendingIntent(R.id.notify, pi);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setAutoCancel(false)
.setContentTitle("title")
.setContentText("test")
.setSmallIcon(R.mipmap.ic_launcher)
.setOngoing(true) // 左、右滑是否删除
.setCustomContentView(remoteViews)
.setWhen(System.currentTimeMillis())
.build();
assert manager != null;
manager.notify(0x01, notification);
}
...
注意
RemoteViews 使用有限制:
- 布局: LinearLayout,RelativeLayout,FrameLayout,GridLayout
- 控件: AnalogClock,Button,Chronometer,ImageButton,ImageView,ProgressBar,TextView,ViewFlipper,ListView,GridView,StackView,AdapterViewFlipper
- 只支持这些View,不包括其子类,以及自定义的View。
桌面小部件
Step :
- 新建小部件的布局
- 新建小部件配置信息(xml/xxx.xml)
- 创建A类继承自AppWidgetProvider,重写各方法
- (update中)指定Intent(设置Action),通过PendingIntent获取pi.getBroadcast(....)
- 实例化RemoteViews,设置点击事件等
- 调用updateAppWidget(appWidgetId, remoteViews)
- onReceiver中比较ACTION并分发事件
- AndroidManifest.xml中配置receiver
...
public class MyAppWidgetProvider extends AppWidgetProvider
{
public static final String ACTION = "com.wdl.android.action.CLICK";
private static int count = 0;
/**
* 每次桌面小部件更新时都调用
*
* @param context Context
* @param appWidgetManager AppWidgetManager
* @param appWidgetIds int
*/
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
super.onUpdate(context, appWidgetManager, appWidgetIds);
for (int appWidgetId : appWidgetIds)
{
updateWidget(context, appWidgetManager, appWidgetId);
}
}
static void updateWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId)
{
// 设置ACTION 获取广播
Intent intent = new Intent(ACTION);
PendingIntent pi = PendingIntent.getBroadcast(context, 0x01, intent, 0);
// 实例化RemoteViews
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.remoteviews_appwidget_layout);
remoteViews.setTextViewText(R.id.textView, "" + (count++));
remoteViews.setOnClickPendingIntent(R.id.textView, pi);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
/**
* 第一次添加时调用
*
* @param context Context
*/
@Override
public void onEnabled(Context context)
{
super.onEnabled(context);
}
/**
* 最后一个该类型的的桌面小部件被删除时调用
*
* @param context Context
*/
@Override
public void onDisabled(Context context)
{
super.onDisabled(context);
}
/**
* 广播接收器
* @param context Context
* @param intent Intent
*/
@Override
public void onReceive(Context context, Intent intent)
{
super.onReceive(context, intent);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.remoteviews_appwidget_layout);
if (intent.getAction().equals(ACTION))
{
// 分发事件
AppWidgetManager manager = AppWidgetManager.getInstance(context);
ComponentName componentName = new ComponentName(context, MyAppWidgetProvider.class);
manager.updateAppWidget(componentName, remoteViews);
}
}
/**
* 每删除一次调用一次
*
* @param context Context
* @param appWidgetIds int
*/
@Override
public void onDeleted(Context context, int[] appWidgetIds)
{
super.onDeleted(context, appWidgetIds);
}
}
<appwidget-provider
xmlns:android="http:// schemas.android.com/apk/res/android"
android:updatePeriodMillis="30000"
android:minHeight="84dp"
android:minWidth="84dp"
android:initialLayout="@layout/remoteviews_appwidget_layout"
/>
<?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="10dp">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/app_name"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
<receiver android:name=".remoteviews.provider.MyAppWidgetProvider">
<intent-filter>
<action android:name="com.wdl.android.action.CLICK" />
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/appwidget_provider_info">
</meta-data>
</receiver>
...
桌面长按点击添加小部件即可验证是否成功!!
其中各个回调的作用如下:
方法名 | 调用时机 |
---|---|
onDeleted | 每删除一次调用一次 |
onReceive | 广播接收 |
onDisabled | 最后一个该类型的的桌面小部件被删除时调用 |
onEnabled | 第一次添加时调用 |
onUpdate | 每次桌面小部件更新时都调用 |