When developing appwidget, We need to write a class to inherit the appwidgetprovider. This article analyzes the source code of appwidgetprovider.
First, appwidgetprovider inherits broadcastreceiver, so it is essentially a broadcast receiver, especially it mainly receives some broadcasts about appwidgets sent by appwidgetservice.
Secondly, it only has an empty constructor and does not have any member variables. There are only five methods. Of course, one of them is the onreceive method, which must be implemented as a broadcast receiver. The other four methods are onupdate, ondeleted, onenabled, and ondisabled, but the four methods are empty. The user inherits the appwidgetprovider and can implement these methods as desired.
Let's take a look at the onreceive method. First, attach the source code and analyze it.
public void onReceive(Context context, Intent intent) { 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_ENABLED.equals(action)) { this.onEnabled(context); } else if (AppWidgetManager.ACTION_APPWIDGET_DISABLED.equals(action)) { this.onDisabled(context); } }
The code is actually very simple. It extracts the action carried by the received intent, and then calls its own four methods based on the different actions. The four actions are defined in appwidgetmanager as follows:
public static final String ACTION_APPWIDGET_UPDATE = "android.appwidget.action.APPWIDGET_UPDATE"; public static final String ACTION_APPWIDGET_DELETED = "android.appwidget.action.APPWIDGET_DELETED"; public static final String ACTION_APPWIDGET_DISABLED = "android.appwidget.action.APPWIDGET_DISABLED"; public static final String ACTION_APPWIDGET_ENABLED = "android.appwidget.action.APPWIDGET_ENABLED";
Note that when writing an appwidget to inherit the appwidgetprovider, override onupdate is required. If not, your appwidget will not take effect, I have not figured it out. Although the appwidgetprovider class we inherit is essentially a broadcast receiver, we can call this method of the parent class without override onreceive, of course, if you want to receive some other broadcasts, you must repeat this method and add your own judgment.
There are still some problems that need to be explained: 1. who will send the broadcast to appwidgetprovider; 2. when to send a broadcast? 3. why are we in manifest. only one action: Android. appwidget. action. appwidget_update, but can receive four types of broadcasts. The following is a part of manifest. xml. You can see that there is only one action in intent-filter:
<receiver android:name="exampleAppWidgetProvider"> <intent-filter > <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/example_appwidget_info"/></receiver>
The first problem is that appwidgetservice is sending a broadcast. appwidgetservice is responsible for managing all appwidgets, and its interaction with appwidgetprovider is through the sending broadcast mechanism. Generally, when an appwidget is created by a mobile phone user for the first time, an enabled and update broadcast will be sent. If you create another appwidget, only one update broadcast will be sent. When a mobile phone user deletes an appwidget, a deleted broadcast will naturally be received. If this appwidget does not exist on the mobile phone, a disabled broadcast will be sent again, in addition, each appwidget has its own Update Time, which generally takes more than half an hour to be updated. When the update time is reached, an update broadcast will be sent. However, it is said that the current android
4.0 does not have this function. I do not know the details. To learn more about broadcast sending, you 'd better go to appwidgetservice.