RemoteViews入门

RemoteViews

RemoteViews表示的是一个View结构,实现了Parceable接口-它可以在其他进程中显示;使用场景:通知、桌面小部件.

一、RemoteViews的应用

  • 通知
  • 桌面小部件

通知

Step :

  1. 指定Intent,通过PendingIntent获取pi
  2. 获取NotificationManager(Android O及以上需设置Channel)
  3. 通过Channel创建Manager
  4. 实例化RemoteViews
  5. 创建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 使用有限制:

  1. 布局: LinearLayout,RelativeLayout,FrameLayout,GridLayout
  2. 控件: AnalogClock,Button,Chronometer,ImageButton,ImageView,ProgressBar,TextView,ViewFlipper,ListView,GridView,StackView,AdapterViewFlipper
  3. 只支持这些View,不包括其子类,以及自定义的View。

桌面小部件

Step :

  1. 新建小部件的布局
  2. 新建小部件配置信息(xml/xxx.xml)
  3. 创建A类继承自AppWidgetProvider,重写各方法
  4. (update中)指定Intent(设置Action),通过PendingIntent获取pi.getBroadcast(....)
  5. 实例化RemoteViews,设置点击事件等
  6. 调用updateAppWidget(appWidgetId, remoteViews)
  7. onReceiver中比较ACTION并分发事件
  8. 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);
}
}












        
            
            
        

        

        
    

...

桌面长按点击添加小部件即可验证是否成功!!

其中各个回调的作用如下:

方法名 调用时机
onDeleted 每删除一次调用一次
onReceive 广播接收
onDisabled 最后一个该类型的的桌面小部件被删除时调用
onEnabled 第一次添加时调用
onUpdate 每次桌面小部件更新时都调用

你可能感兴趣的:(RemoteViews入门)