Detailed Explanation of AppWidget Developed by Android

  • 2021-12-12 05:45:41
  • OfStack

Android notification system is one of its major features, and AppWidget is one of its highlights. In the development of applications, a lot of time can be added to its 1 AppWidget display in the desktop, timely and convenient with the user

Interaction. Here is a simple familiar with the process of developing an AppWidget under 1.

To create an AppWidget in an application, you need at least the following things:

You need to create an AppWidgetProviderInfo to describe the metadata of AppWidget. Need to implement a own AppWidgetProvider to update AppWidget and other operations. A layout file is required to describe the layout of AppWidget.

So, let's start creating an AppWidget.

1. Declare an AppWidget in AndroidManifest. xml

First we need to declare AppWidgetProvider in AndroidManifest. xml. The format is as follows:


<receiver android:name="MyAppWidgetProvider" >  
    <intent-filter>  
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />  
    </intent-filter>  
    <meta-data android:name="android.appwidget.provider"  
               android:resource="@xml/my_appwidget_info" />  
</receiver>  

It can be seen that AppWidgetProvider is actually an BroadcastReceiver, which receives a specific Broadcast. < meta-data > The tag describes the metadata used by AppWidget, and android: resource declares the location of the xml file that defines the metadata.

2. Add AppWidgetProviderInfo metadata

AppWidgetProviderInfo describes the essential characteristics of AppWidget, such as the duration of AppWidget updates, the minimum width, length, what layout files are used, and what needs to be started to add AppWidget

configuration Activity, etc. We need to define the AppWidgetProviderInfo object in XML, and this XML file should be saved in the res/xml folder. Here is an example:


<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"  
    android:minWidth="294dp"  
    android:minHeight="72dp"  
    android:updatePeriodMillis="86400000"  
    android:previewImage="@drawable/preview"  
    android:initialLayout="@layout/example_appwidget"  
    android:configure="com.example.android.MyAppWidgetConfigure"   
    android:resizeMode="horizontal|vertical">  
</appwidget-provider>  

< appwidget-provider > You need to use this tag to define AppWidgetProviderInfo. The attributes used in the example are described below.

minWidth and minHeight define the minimum space that AppWidget needs to occupy.

updatePeriodMillis defines how often AppWidget needs to be updated once, but only one approximate time is defined here, and the system cannot make accurate guarantee.

previewImage defines the icon that makes reality when the user selects AppWidget.

initialLayout defines the layout files used by AppWidget.

configure defines the configuration Activity that needs to be started when AppWidget is added to perform configuration work.

resizeMode defines the zoom mode.

3. Create the layout file used by AppWidget

When creating AppWidget, you must create a layout file to provide a layout description for it. When AppWidget creates a view, it needs to be created according to RemoteViews. For the sake of efficiency and other factors, many controls are in

Is supported in RemoteViews. The following is a list of UI controls that can be used in RemoteViews:

layout : FrameLayout , LinearLayout , RelativeLayout

widget : AnalogClock , Button , Chronometer , ImageButton , ImageView , ProgressBar , TextView , ViewFlipper , ListView , GridView , StackView , AdapterViewFlipper

4. Create a subclass of AppWidgetProvider

As mentioned earlier, AppWidgetProvider is an BroadcastReceiver. Yes, it is actually inherited from BroadcastReceiver, but it is encapsulated for more convenient processing of AppWidget broadcasts.

When AppWidgetProvider receives the broadcast of AppWidget, it will trigger the following methods according to the type:

onUpdate (): This method is triggered when AppWidget needs to be updated, and we need to override this method to implement the update operation in it. If configuration Activity is not defined, add 1 AppWidget

This method is also triggered when.

onDelete (Context, int []): This method is triggered when AppWidget is removed from AppWidgetHost.

onEnabled (Context): If more than one AppWidget is added to an application, this method will only be called if the first AppWidget is added.

onDisabled (Context): This method is triggered when the last AppWidget of an application is removed from AppWidgetHost.

onReceive (Context, Intent): This is actually the method in BroadcastReceiver, which is called when any 1 Broadcast is received and is called before the above callback method.

5. Create an ConfigurationActivity (optional)

If you need to do some configuration work when adding AppWidget, you can use Configuration Activity. To use ConfigurationActivity, you first need to look like the normal Activity1 in AndroidManifest. xml

Make a statement:


<activity android:name=".ExampleAppWidgetConfigure">  
    <intent-filter>  
        <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>  
    </intent-filter>  
</activity>  

Only here you need to add intent-filter of action type android. appwidget. action.APPWIDGET_CONFIGURE. Then, you need to declare it in AppWidgetProviderInfo:


<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"  
    ...  
    android:configure="com.example.android.ExampleAppWidgetConfigure"   
    ... >  
</appwidget-provider>  

Finally, of course, you need to create Activity. In Configuration Activity, you need to perform one necessary operation:

1. Get AppWidget ID


Intent intent = getIntent();  
Bundle extras = intent.getExtras();  
if (extras != null) {  
    mAppWidgetId = extras.getInt(  
            AppWidgetManager.EXTRA_APPWIDGET_ID,   
            AppWidgetManager.INVALID_APPWIDGET_ID);  
}  

2. Carry out necessary configuration operation, obtain AppWidgetManager instance and update RemoteViews


AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);  
 
RemoteViews views = new RemoteViews(context.getPackageName(),  
R.layout.example_appwidget);  
appWidgetManager.updateAppWidget(mAppWidgetId, views);  

3. Set Activity result and return 1 Intent.


Intent resultValue = new Intent();  
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);  
setResult(RESULT_OK, resultValue);  
finish();  

This creates an Configuration Activity.

Note that after android8.0, you can't receive AppWidgetProvider sent to you, so you need to add it


intent.setComponent(new ComponentName(context,CacheProvider.class));

Intent intent = new Intent();
intent.setAction(ACTION_CACHE_CLEAN);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setComponent(new ComponentName(context,CacheProvider.class));
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                    0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.tv_clean, pendingIntent);

After performing the above steps, you have created an AppWidget that can be displayed on the desktop.

These are the details of AppWidget developed by Android. For more information about Android AppWidget, please pay attention to other related articles on this site!


Related articles: