RemoteViews

RemoteViews represents a View structure that implements the Parceable interface – it can be displayed in other processes; Usage scenarios: notifications, desktop widgets.

1. Application of RemoteViews

  • notice
  • Desktop widgets

notice

Step:

  1. Specify an Intent, and retrieve the PI via PendingIntent
  2. Get NotificationManager(set Channel for Android O and above)
  3. Create a Manager through a Channel
  4. Instantiate the RemoteViews
  5. Create a Notifycation(setting the instance into it) and notify

. Private void remoteViewsNotify() {// Specify intent intent intent = new intent (this, notifyActivity.class); / / equivalent to startActivity (intent) PendingIntent PI = PendingIntent. GetActivity (this, 0 x01, intent, PendingIntent.FLAG_UPDATE_CURRENT);

// get NotificationManager NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 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.i which extView, "new message"); 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); }Copy the code

.

Pay attention to

RemoteViews uses restrictions:

  1. Layout: LinearLayout, RelativeLayout, FrameLayout, GridLayout
  2. Controls: AnalogClock, Button, Chronometer, ImageButton, ImageView, ProgressBar, TextView, ViewFlipper, ListView, GridView, StackView, AdapterViewFlipper
  3. Only these views are supported, excluding their subclasses and custom views.

Desktop widgets

Step:

  1. The layout of the new widget
  2. New Widget configuration Information (XML/XXX.xml)
  3. Create class A that inherits from AppWidgetProvider and overrides the methods
  4. PendingIntent = pi.getBroadcast(….)
  5. Instantiate RemoteViews, set click events, etc
  6. Call updateAppWidget (appWidgetId, remoteViews)
  7. OnReceiver compares actions and distributes events
  8. The receiver is configured in androidmanifest.xml

.

public class MyAppWidgetProvider extends AppWidgetProvider { public static final String ACTION = "com.wdl.android.action.CLICK"; private static int count = 0; /** * is called every time a desktop widget is updated ** @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, Intintent = new Intent(ACTION); intIntent = new Intent(ACTION); PendingIntent pi = PendingIntent.getBroadcast(context, 0x01, intent, 0); 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); Override public void onEnabled(context context) {Override public void onEnabled(context context) { super.onEnabled(context); Override public void onDisabled(context context) {Override public void onDisabled(context context) {Override public void onDisabled(context context) { super.onDisabled(context); } @override public void onReceive(context, intent, intent, intent, intent) Intent intent) { super.onReceive(context, intent); RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.remoteviews_appwidget_layout); If (intent. GetAction (). The equals (ACTION)) {/ / distribute events AppWidgetManager manager = AppWidgetManager. GetInstance (context); ComponentName componentName = new ComponentName(context, MyAppWidgetProvider.class); manager.updateAppWidget(componentName, remoteViews); }} /** * is called once for each deletion ** @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>Copy the code

.

Desktop long press and click add widget to verify success!!

The functions of each callback are as follows:

The method name Call time
onDeleted This is called once for each deletion
onReceive Radio reception
onDisabled Called when the last desktop widget of this type is deleted
onEnabled Called the first time you add it
onUpdate Called every time a desktop widget is updated