Android widget desktop component development
Widget is one of the features introduced by Android version 1.5. Widget allows users to understand important information displayed by the program in the home screen interface. The standard Android system has included several examples of Widgets, such as analog clocks, music players, etc.
1. AppWidget framework class
1、AppWidgetProvider : Inherited from BroadcastRecevier , receive notifications when AppWidget applies update, enable, disabled and delete. Among them, onUpdate and onReceive are the most commonly used methods, and they receive update notifications.
2、 AppWidgetProvderInfo: Describes the size, update frequency and initial interface of the AppWidget, and exists in the application's res/xml/ directory in the form of an XML file.
3、AppWidgetManger : Responsible for managing AppWidgets and sending notifications to AppwidgetProvider.
4、RemoteViews: A class that can be run in other application processes, sending notifications to AppWidgetProvider.
2. Introduction to the main categories of AppWidget framework
1)AppWidgetManger Class
bindAppWidgetId(int appWidgetId, ComponentName provider)
Bind appWidgetId via the given ComponentName
getAppWidgetIds(ComponentName provider)
Get the AppWidgetId via the given ComponentName
getAppWidgetInfo(int appWidgetId)
Get AppWidget information through AppWidgetId
getInstalledProviders()
Returns the information of List<AppWidgetProviderInfo>
getInstance(Context context)
Get the context object used by the AppWidgetManger instance
updateAppWidget(int[] appWidgetIds, RemoteViews views)
Modify the incoming RemoteView through appWidgetId and refresh the AppWidget component
updateAppWidget(ComponentName provider, RemoteViews views)
Modify the incoming RemoeteView through ComponentName and refresh the AppWidget component
updateAppWidget(int appWidgetId, RemoteViews views)
Modify the incoming RemoteView through appWidgetId and refresh the AppWidget component
2) Inherited fromAppWidgetProvider The possible methods are as follows:
1、onDeleted(Context context, int[] appWidgetIds)
2、onDisabled(Context context)
3、onEnabled(Context context)
4、onReceive(Context context, Intent intent)
Tip: Because AppWidgetProvider is inherited from BroadcastReceiver, you can rewrite the onRecevie method, of course, you must register Receiver in the background.
5、onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
Three, Demo Explained
1. Create the Widget content provider file. We create the xml folder under res, and create a new widget_provider.xml code into:
<?xml version="1.0" encoding="utf-8"?> <appwidget-provider xmlns:andro android:minWidth="50dip" android:minHeight="50dip" android:updatePeriodMillis="10000" android:initialLayout="@layout/main" />
Tip: As mentioned above, AppWidgetProvderInfo exists in the file form of res/xml. It is not difficult to understand based on the parameters. What is more important is that here android:initialLayout="@layout/main" This sentence is the layout file for the specified desktop component.
The main parameters are as follows:
minWidth:Define the width of the Wdiget component
minHeight: Define the height of the Wdiget component
updatePeriodMillis:Update time period
initialLayout: WidgetLayout file
configure:If you need to start an Activity before starting and set it, here is the complete class name of the Activity (it will be mentioned later, it is somewhat different from the implementation of general Activity)
*Widget size calculation: (number of cells*74)-2, the API says it is to prevent integer rounding during pixel calculations, so -2...I don't understand very well
2. Modify the layout, the code is as follows:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/wordcup" > <TextView android: android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:textSize="12px" android:textColor="#ff0000" /> </LinearLayout>
Tips: The XML file that defines the Widget interface layout (located in res/layout/..). It should be noted that the components used must be supported by RemoteViews. The components supported in the current native API are as follows:
FrameLayout、LinearLayout、RelativeLayout
AnalogClock、Button、Chronmeter、ImageButton、ImageView、ProgressBar、TextView
*If a component other than this is used, an exception will be caused when the widget is created.
PS: This leads to some functions or styles that cannot be implemented, such as very basic list or text editing boxes that cannot be directly implemented. If you want to customize the View in the Widget, you can only provide support for the corresponding components by modifying the framework.
3. Write a class inherited from AppWidgetProvider
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class WidetDemo extends AppWidgetProvider { /** Called when the activity is first created. */ @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { Timer timer = new Timer(); (new MyTime(context,appWidgetManager), 1, 60000); (context, appWidgetManager, appWidgetIds); } private class MyTime extends TimerTask{ RemoteViews remoteViews; AppWidgetManager appWidgetManager; ComponentName thisWidget; public MyTime(Context context,AppWidgetManager appWidgetManager){ = appWidgetManager; remoteViews = new RemoteViews((),); thisWidget = new ComponentName(context,); } public void run() { Date date = new Date(); Calendar calendar = new GregorianCalendar(2010,06,11); long days = (((()-())/1000))/86400; (, "It's still away from the South Africa World Cup" + days+"sky"); (thisWidget, remoteViews); } } }
AppWidgetProvider is actually a BroadcastReceiver, which provides the following functions:
onReceive(Context, Intent)
onUpdate(Context , AppWidgetManager, int[] appWidgetIds)
onEnabled(Context)
onDeleted(Context, int[] appWidgetIds)
onDisabled(Context)
You can listen to changes in the Widget state by rewriting the above functions and perform corresponding processing.
onUpdate is called when the component is generated on the desktop and updates the component UI. onReceiver is called when the broadcast is received. Generally, these two methods are more commonly used.
Widget updates are different from Activity and must be achieved by RemoteViews and AppWidgetMananger.
Function call cycle [start up - noneconfiure Activity] onReceive onEnabled —— The firstwidgetBeing displayed onReceive onUpdate —— Refresh the interface [start up - bringconfiuration Activity] onReceive onUpdate [drag] <none状态变化> [Periodic update] onReceive onUpdate [delete] onReceive onDeleted —— widget被delete onReceive onDisabled —— The last onewidgetRemoved [start up时位置不够] onReceive onEnabled onReceive onUpdate onReceive onDeleted onReceive onDisabled
*Each state change will trigger onReceive, and generally this function does not need to be rewrited.
4. Modify the configuration file and register the Receiver in the background. The code is as follows:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:andro package="" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <receiver android:name=".WidetDemo" android:label="@string/app_name"> <intent-filter> <action android:name=".APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="" android:resource="@xml/widget_provider" /> </receiver> </application> <uses-sdk android:minSdkVersion="7" /> </manifest>
Tips:
Because it is a desktop component, you will not consider using the Activity interface for the time being. Of course, when you implement the project, you may need to click and jump to the Activity application to do operations. A typical case is the music player provided by Android.
The more important sentence in the above code is this sentence<meta-data android:name="" android:resource="@xml/appwidget_provider"></meta-data> The general meaning is to specify the AppWidgetProvderInfo file of the desktop application, so that it can be used as its management file.
5. Add modification resources
Add picture material.
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, WidetDemo!</string> <string name="app_name">DaysToWorldCup</string> </resources>
The above is a compilation of Android widget information, hoping to help friends in need.