Android AppWidget development practices, androidappwidget

Source: Internet
Author: User

Android AppWidget development practices, androidappwidget

AppWidget framework class 1. AppWidgetProvider

It inherits from BroadcastRecevier and receives notifications when AppWidget applies update, enable, disable, and delete. OnUpdate and onReceive are the most common methods. They receive update notifications.
The methods inherited from AppWidgetProvider are as follows:
OnDeleted (Context context, int [] appWidgetIds)
OnDisabled (Context context)
OnEnabled (Context context)
OnReceive (Context context, Intent intent)
OnUpdate (Context context, AppWidgetManager appWidgetManager, int [] appWidgetIds)

2. AppWidgetProvderInfo

Describes the size, update frequency, and initial interface of an AppWidget, which is stored in the res/XML/directory of the application as an xml file.

3. AppWidgetManger

Manages appwidgets and sends notifications to AppwidgetProvider.
BindAppWidgetId (int appWidgetId, ComponentName provider): bind appWidgetId through the specified ComponentName
GetAppWidgetIds (ComponentName provider): obtains AppWidgetId through the specified ComponentName.
GetAppWidgetInfo (int appWidgetId): obtains AppWidget information through AppWidgetId.
GetInstalledProviders (): returns the information of a List <AppWidgetProviderInfo>.
GetInstance (Context context): gets the Context object used by the AppWidgetManger instance.
UpdateAppWidget (int [] appWidgetIds, RemoteViews views): Modify the passed RemoteView through appWidgetId, and refresh the AppWidget component.
UpdateAppWidget (ComponentName provider, RemoteViews views): Use ComponentName to modify the passed RemoeteView and refresh the AppWidget component.
UpdateAppWidget (int appWidgetId, RemoteViews views): Modify the passed RemoteView through appWidgetId, and refresh the AppWidget component.

4. RemoteViews

A class that can run in other application processes sends notifications to AppWidgetProvider.

Demo

A simple AppWidget step

1. Create an xml folder under res, and then create appwidget_provider.xml

<?xml version="1.0" encoding="UTF-8"?><appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"android:minWidth="60dp"android:minHeight="30dp"android:updatePeriodMillis="86400000"    android:initialLayout="@layout/widget_main"></appwidget-provider>
2. Create the layout file widget_main.xml in the layout folder.
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: id = "@ + id/layout" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: orientation = "vertical"> <TextView android: id = "@ + id/TV" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: clickable = "true" android: text = "Point me"> </TextView> </LinearLayout>
3. The definition class is inherited from AppWidgetProvider.
Public class MyAppWidgetProvider extends AppWidgetProvider {private RemoteViews remoteViews; @ Override public void onReceive (Context context, Intent intent) {super. onReceive (context, intent); if (remoteViews = null) {remoteViews = new RemoteViews (context. getPackageName (), R. layout. widget_main);} if (intent. getAction (). equals ("com. example. widgettest ") {if (CommonValiable. isChange) {remoteVie Ws. setTextViewText (R. id. TV, "click me");} else {remoteViews. setTextViewText (R. id. TV, "buy, buy, and buy me");} Toast. makeText (context, Boolean. toString (CommonValiable. isChange), Toast. LENGTH_LONG ). show (); CommonValiable. isChange =! CommonValiable. isChange;} AppWidgetManager appWidgetManger = AppWidgetManager. getInstance (context); int [] appIds = appWidgetManger. getAppWidgetIds (new ComponentName (context, MyAppWidgetProvider. class); appWidgetManger. updateAppWidget (appIds, remoteViews) ;}@ Override public void onUpdate (Context context, AppWidgetManager appWidgetManager, int [] appWidgetIds) {final int N = appWidgetIds. length; for (int I = 0; I <N; I ++) {int appWidgetId = appWidgetIds [I]; updateAppWidget (context, appWidgetManager, appWidgetId );}} public void updateAppWidget (Context context, AppWidgetManager appWidgeManger, int appWidgetId) {remoteViews = new RemoteViews (context. getPackageName (), R. layout. widget_main); Intent intent = new Intent ("com. example. widgettest "); PendingIntent pendingIntent = PendingIntent. getBroadcast (context, 0, intent, 0); remoteViews. setOnClickPendingIntent (R. id. TV, pendingIntent); appWidgeManger. updateAppWidget (appWidgetId, remoteViews );}}
4. declare the following under the application node of the configuration file:

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

Download the Demo.



In android development, how does one make the appWidget generate a dialog box when it is clicked?

It may not work independently, but you can make the dialog of your activity and configure the entire activity in manifest as a dialog.
Set this attribute in activity.
Android: theme = "@ android: style/Theme. Dialog

Crazy Android handout (version 2nd) and: Android development practices classic which is more suitable for beginners ??

Crazy android handout is the author of crazy java handout, which has a lot of content and details. Of course, if you have a good java Foundation, we recommend you read this book.

If you are a beginner and do not know the java language, you can choose android development practices classic, which is more suitable for you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.