# Read Android Development Art Exploration &8

Keywords: Android xml github

Key words: RemoteViews / Notification Bar / Desktop Widget/

RemoteViews is a remote View, a remote Service, which is actually the same as a remote Service. RemoteViews is a View structure that can be displayed in other processes and provides a set of operations to update its interface across processes. There are two scenarios for RemoteViews in Android: notification bars and desktop widgets.

RemoteViews are used in the development of notification bars and desktop widgets. It is impossible to update the View directly when updating the interface as it does in Activity, because both interfaces run in other processes, i.e. the System Server process of the system. To update the interface across processes, RemoteViews provides a series of set methods, and these methods are just all View methods. A subset of the View types supported in RemoteViews are also limited

1. Notification Bar#

The notification bar is mainly implemented by Notification Manager's notify method, which can be customized in addition to the default effect.

The default style of the system is simple:

Notification notification = new Notification();
notification.icon = R.drawable.ic_launcher;
notification.tickerText = "hello world";
notification.when = System.currentTimeMillis();
notification.flags = Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(this, DemoActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(this, "hello", "this is a notification.", pendingIntent);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, notification);

Custom styles are also simple. Load the layout file through RemoteViews:

Notification notification = new Notification();
notification.icon = R.drawable.ic_launcher;
notification.tickerText = "hello world";
notification.when = System.currentTimeMillis();
notification.flags = Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(this, DemoActivity.class);
/ 'PendingIntent The expression is undetermined. Intent,this Intent The intention contained must be triggered by the user'
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.layout_notification);
remoteViews.setTextViewText(R.id.msg, "hello");
remoteViews.setImageResource(R.id.icon, R.drawable.icon);
PendingIntent openActivity2PendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, DemoActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.open_activity2, openActivity2PendingIntent);
notification.contentView = remoteViews;
notification.contentIntent = pendingIntent;

NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(2, notification);

2. Desktop widgets#

Desktop widgets are implemented by AppWidgetProvider, which is essentially a broadcast component, so they must be registered.

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

    <intent-filter>
        <action android:name="io.github.isayes.action.CLICK" />
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
    </intent-filter>
</receiver>

Development steps;
- Define widget interfaces
- Define widget configuration information
- Define implementation classes for widgets
- Declare widgets in anifest files

3. A few points about Pending Intent#

  1. Express the intention of a pending state, waiting, forthcoming meaning;
  2. Pending Intent happens at some uncertain time in the future, and Intent happens immediately.
  3. Typical usage scenarios are adding click events to RemoteViews, sending and canceling specific undetermined Intent s through send and cancel methods.
  4. Support three undetermined intentions: start Activity, start Service and send broadcasting;
  5. RemoteViews does not support all View types, specifically Technological Exploration P230;
  6. There is no findViewById method to access the View elements directly, but it must be completed through a series of set methods provided by RemoteViews, because RemoteViews are displayed in the remote process and can not find the ViewById directly.
  7. In fact, most set methods are done by reflection.
  8. RemoteViews implements the Parcelable interface.
  9. For click events, only Pending Intent is supported in RemoteViews, and onClickListener is not supported.

Not End.

Posted by glitch003 on Sat, 23 Mar 2019 01:51:53 -0700