AppWidget and androidappwidget
AppWidget is a desktop Window widget that allows you to perform operations on the widget (depending on your needs ). As a cainiao, I will introduce the simple use of AppWeight here.
1. Before introducing AppWidget, let's take a look at PendingIntent and RemoteViews;
PendingIntent:A description of an Intent and target action to perform with it. Instances of this class are createdgetActivity
,getActivities
,getBroadcast
, AndgetService
The returned object can be handed to other applications so that they can perform the action you described on your behalf at a later time.
Translation:PendingIntent is used to describe an Intent and target action. Instances of this class are created together with getActivity, getActivites, getBroadCast, and getService: The returned objects can be transmitted to other programs, so that they can execute the actions you want them to perform later.
This can be understood in conjunction with the explanation given by the instructor Mars:PendingIntent can contain an Intent object, which can be passed to other applications. when certain conditions are met, the action contained in this Intent will be executed.GetActivity and getActivities are used to start the activity. getBroadcast is used to send broadcasts, and getService is used to enable a service. (This is explained below)
RemoteViews:A class that describes a view hierarchy that can be displayed in another process. the hierarchy is inflated from a layout resource file, and this class provides some basic operations for modifying the content of the inflated hierarchy.
Remoteviews: A class describes the hierarchy of a view, which can be displayed in another process. This hierarchy increases from the layout resource file. This class provides some basic operations to modify the content of the virtual hierarchy.
In short, RemoteView represents the entire layout of the appwidget displayed on the desktop, including the set button, image, and so on...
2. Understand the above concepts to better understand AppWeight.
Steps for AppWeige implementation:
File structure:
1) create a folder under res, and create an xml file of the appwidget_provider type. This indicates the entire layout displayed on the desktop (only the layout framework does not contain details, and the displayed content must reference another layout file). In this example, my xml file name is example_appwidget_provider.xml:
<? Xml version = "1.0" encoding = "UTF-8"?> <Appwidget-provider xmlns: android = "http://schemas.android.com/apk/res/android" android: minWidth = "280dp" android: minHeight = "100dp" android: updatePeriodMillis = "864000000" android: initialLayout = "@ layout/example_appwidget"> </appwidget-provider> <! -- The layout width, height, Update time period (MS), and included layout are set above -->
2) create an xml file in layout to display the layout of the appwidget, that is, example_appwidget in @ layout/example_appwidget above.
<? 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"> <! -- The layout of the appWidget displayed on the desktop --> <ImageView android: id = "@ + id/imageId" android: layout_width = "fill_parent" android: layout_height = "60dp" android: src = "@ drawable/cancer"/> <Button android: id = "@ + id/changeButton" android: layout_width = "match_parent" android: layout_height = "40dp" android: textColor = "#00ff00" android: text = "enable another activity"/> </LinearLayout>
3) create an Activity that inherits the AppWidgetProvider and rewrites the method. The annotated code enables you to click the button in AppWidget to start another Activity. unannotated code replaces the Image in ImagView: It is worth noting that,The activity carried out by AppWiget is not in the same thread as the thread of the current Main_Activity. Therefore, the layout content cannot be operated as in the main thread.The operation method of appwiet can be viewed in the following code.
Package com. example. appwight; import android. app. pendingIntent; import android. appwidget. appWidgetManager; import android. appwidget. appWidgetProvider; import android. content. componentName; import android. content. context; import android. content. intent; import android. widget. remoteViews; public class ExampleAppWidgetProvider extends AppWidgetProvider {// broadcast name, which must be in AppWidget Manifest. declare private static final String UPDATE_INTENT = "com. example. appwidget "; @ Override public void onReceive (Context context, Intent intent) {// call String action = intent when receiving broadcast. getAction (); if (UPDATE_INTENT.equals (action) {System. out. println ("action =====>" + UPDATE_INTENT); RemoteViews remoteViews = new RemoteViews (context. getPackageName (), R. layout. example_appwidget );
// The first parameter is the Id of ImagView, and the second parameter is the remoteViews parameter used for replacement. setImageViewResource (R. id. imageId, R. drawable. chilli); AppWidgetManager appWidgetManager = AppWidgetManager. getInstance (context); // ComponentName also represents the AppWidget control, which is different from RemoteViews. The former only represents the "entire" control, and the latter contains the ComponentName componentName = new ComponentName (context, ExampleAppWidgetProvider. class); appWidgetManager. updateAppWidget (componentName, remoteViews);} else {// call the onReceive method super of the parent class. onReceive (context, intent) ;}@ Override public void onUpdate (Context context, AppWidgetManager appWidgetManager, int [] appWidgetIds) {super. onUpdate (context, appWidgetManag Er, appWidgetIds); System. out. println ("onUpdate"); for (int I = 0; I <appWidgetIds. length; I ++) {// Intent intent = new Intent (context, SecondActivity. class); Intent intent = new Intent (); intent. setAction (UPDATE_INTENT);/** pendingIntent can contain an intent *. The first parameter is the current context, the second parameter is the contained intent * getacitient, indicating that the PendingIntent is used to start an activity. In addition, getService (), and getBroadcast () * PendingIntent pendingIntent = Pe NdingIntent. getActivity (context, 0, intent, 0); */PendingIntent pendingIntent = PendingIntent. getBroadcast (context, 0, intent, 0); // remoteView indicates the entire layout of AppWidget. The first parameter is the package name, the second parameter is the layout of AppWidget RemoteViews remoteViews = new RemoteViews (context. getPackageName (), R. layout. example_appwidget); // set the click event for the button in remoteVIew. The first is the id of the button, and the second parameter is the PendingIntent to be started. RemoteViews. setOnClickPendingIntent (R. id. changeButton, pendingIntent); // update an AppWidget. The first parameter indicates the current AppWidget (because multiple appwidgets can be created on the desktop) // The second parameter is the appWidgetManager object of the RemoteViews object to be updated. updateAppWidget (appWidgetIds [I], remoteViews) ;}@ Override public void onDeleted (Context context, int [] appWidgetIds) {// call super When deleting an AppWidget. onDeleted (context, appWidgetIds); System. out. println ("onDeleted") ;}@ Override public void onEnabled (Context context) {// call super when the AppWidget is created. onEnabled (context); System. out. println ("onEnabled") ;}@ Override public void onDisabled (Context context) {// call super when the last AppWidget is deleted. onDisabled (context); System. out. println ("onDisable ");}}
4). Declare it in <application in AndroidManifest. xml file.
<Cycler android: name = "com. example. appwight. ExampleAppWidgetProvider">
// Filter broadcast <intent-filter> <action android: name = "android. appwidget. action. APPWIDGET_UPDATE "/> <action android: name =" com. example. appwidget "/> </intent-filter>
// Metadata, bind the layout with the class <meta-data android: name = "android. appwidget. provider" android: resource = "@ xml/example_appwidget_provider"/> </javaser>
: