Preliminary Exploration of Android desktop components & lt; widgets & gt;

Source: Internet
Author: User

 

The outline of this article is as follows:

1. AppWidget framework class

2. How to Use widgets in Android

3. Main Types of AppWidget framework

4. DEMO

1. AppWidget framework class

1. AppWidgetProvider: 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.

2. AppWidgetProvderInfo: Describes the AppWidget size, update frequency, initial interface, and other information. It is stored in the res/XML/directory of the application as an xml file.

3. AppWidgetManger: Manages appwidgets and sends notifications to AppwidgetProvider.

4. RemoteViews: a class that can be run in other application processes. It sends notifications to AppWidgetProvider.

2. How to Use widgets in Android

1. Long-pressed Main Interface

 

2. A dialog box is displayed, which contains some built-in android desktop components.

 

3. Main Types of AppWidget framework

1) AppWidgetManger class

BindAppWidgetId (int appWidgetId, ComponentName provider)

Bind appWidgetId with the given ComponentName

 

GetAppWidgetIds (ComponentName provider)

Obtain AppWidgetId using the given ComponentName

 

GetAppWidgetInfo (int appWidgetId)

Obtain AppWidget information through AppWidgetId

 

GetInstalledProviders ()

Returns the information of a List <AppWidgetProviderInfo>.

 

GetInstance (Context context)

Obtains the context object used by the AppWidgetManger instance.

 

UpdateAppWidget (int [] appWidgetIds, RemoteViews views)

Use appWidgetId to modify the passed RemoteView and refresh the AppWidget component.

 

UpdateAppWidget (ComponentName provider, RemoteViews views)

Modify the passed RemoeteView using ComponentName, and refresh the AppWidget component.

 

UpdateAppWidget (int appWidgetId, RemoteViews views)

Use appWidgetId to modify the passed RemoteView and refresh the AppWidget component.

 

2) The methods inherited from AppWidgetProvider can be implemented 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 inherits from BroadcastReceiver, You can override the onRecevie method. Of course, you must register the Receiver in the background.

5. onUpdate (Context context, AppWidgetManager appWidgetManager, int [] appWidgetIds)

4. Demo

The following is an example of my practice today. It provides reference for everyone. The effect is as follows: Put A TextView in the layout for desktop components, set the Clickable = "true" function of TextView to enable the click function. Then, when we click it, we change its font and click it to return. The detailed procedure is as follows:

1. Create AppWidgetProvderInfo

2. Write a class inherited from AppWidgetProvider

3. Register a Receiver in the background

4. Enable the AppWidget component to support click events

5. How to Make TextView jump back and forth between the two types of Text

The problem is thrown out, So solve it together.

1. Create AppWidgetProvderInfo

The Code is as follows:

 

 

<? 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/main">

</Appwidget-provider>

 

 

Tip: As mentioned above, AppWidgetProvderInfo exists in the res/xml file format. It is not difficult to understand the parameters. More importantly, here is android: initialLayout = "@ layout/main" is the layout file of the specified Desktop component.

2. Write a class inherited from AppWidgetProvider

The main code is as follows:

 

Public class widgetProvider extends AppWidgetProvider

 

 

And rewrite the two methods.

 

@ Override

Public void onUpdate (Context context, AppWidgetManager appWidgetManager,

Int [] appWidgetIds ){}

 

@ Override

Public void onReceive (Context context, Intent intent ){}

 

 

Tip: onUpdate is called when the component is generated on the desktop and updates the component UI. onReceiver calls the update UI when receiving the broadcast. Generally, these two methods are commonly used.

3. Register a Receiver in the background

The background configuration file code is as follows:

 

<Cycler android: name = ". widgetProvider">

<Meta-data android: name = "android. appwidget. provider"

Android: resource = "@ xml/appwidget_provider"> </meta-data>

<Intent-filter>

<Action android: name = "com. terry. action. widget. click"> </action>

<Action android: name = "android. appwidget. action. APPWIDGET_UPDATE"/>

</Intent-filter>

</Cycler>

 

Tip: because it is a desktop component, you do not need to use the Activity interface for the time being. Of course, you may need to jump to the Activity application for operations when you click it during project implementation, A typical case is the music player provided by Android. <Meta-data android: name = "android. appwidget. provider "android: resource =" @ xml/appwidget_provider "> </meta-data> specifies the AppWidgetProvderInfo file of the desktop application to manage the file.

4. Enable the AppWidget component to support click events

First look at the Code:

 

Public static void updateAppWidget (Context context,

AppWidgetManager appWidgeManger, int appWidgetId ){

Rv = new RemoteViews (context. getPackageName (), R. layout. main );

Intent intentClick = new Intent (CLICK_NAME_ACTION );

PendingIntent pendingIntent = PendingIntent. getBroadcast (context, 0,

IntentClick, 0 );

Rv. setOnClickPendingIntent (R. id. TextView01, pendingIntent );

AppWidgeManger. updateAppWidget (appWidgetId, rv );

}

 

This method is used to update the UI called by onUpdate when a component is created. In the code, use RemoteView to find the layout file of the component, at the same time, set the broadcast receiver CLICK_NAME_ACTION for it and locate the TextView for which I want to trigger the event through the setOnClickPendingIntent method of RemoteView. Next

 

@ Override

Public void onReceive (Context context, Intent intent ){

// TODO Auto-generated method stub

Super. onReceive (context, intent );

 

If (rv = null ){

Rv = new RemoteViews (context. getPackageName (), R. layout. main );

}

If (intent. getAction (). equals (CLICK_NAME_ACTION )){

If (uitil. isChange ){

Rv. setTextViewText (R. id. TextView01, context. getResources ()

. GetString (R. string. load ));

 

} Else {

Rv. setTextViewText (R. id. TextView01, context. getResources ()

. GetString (R. string. change ));

 

}

Toast. makeText (context, Boolean. toString (uitil. isChange ),

Toast. LENGTH_LONG). show ();

Uitil. isChange =! Uitil. isChange;

 

}

AppWidgetManager appWidgetManger = AppWidgetManager

. GetInstance (context );

Int [] appIds = appWidgetManger. getAppWidgetIds (new ComponentName (

Context, widgetProvider. class ));

AppWidgetManger. updateAppWidget (appIds, rv );

}

 

 

In oncycler, the broadcast is judged to trigger the action.

 

5. How to Make TextView jump back and forth between the two types of Text

How can TextView return in two states? This is also one of the longest debugging difficulties. The problem lies in the lack of in-depth understanding of AppWidget. If my idea is correct, the life cycle of AppWidget should end with a life cycle after each broadcast execution is received. That is to say, you declare the global variable in the rewritten AppWidgetProvider class for status judgment, every time the status changes, AppWidgetProvider reinitializes the app for you when it receives the second broadcast, that is, the table is reinstantiated for you once again. Today, because I put a boolean value in it and initialized it to "true", we can see that each entry is TRUE. Therefore, when you set the Desktop component, the global variable declares it in another entity class to determine whether it is OK. Do not place it in this class. For the code, refer to the onReceiver method.

As follows:

 

Code:

 

Code

Package com. terry;

 

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;

Import android. widget. Toast;

 

Public class widgetProvider extends AppWidgetProvider {

Private static final String CLICK_NAME_ACTION = "com. terry. action. widget. click ";

 

Private static RemoteViews rv;

 

@ Override

Public void onUpdate (Context context, AppWidgetManager appWidgetManager,

Int [] appWidgetIds ){

// TODO Auto-generated method stub

Final int N = appWidgetIds. length;

For (int I = 0; I <N; I ++ ){

Int appWidgetId = appWidgetIds [I];

UpdateAppWidget (context, appWidgetManager, appWidgetId );

}

}

 

@ Override

Public void onReceive (Context context, Intent intent ){

// TODO Auto-generated method stub

Super. onReceive (context, intent );

 

If (rv = null ){

Rv = new RemoteViews (context. getPackageName (), R. layout. main );

}

If (intent. getAction (). equals (CLICK_NAME_ACTION )){

If (uitil. isChange ){

Rv. setTextViewText (R. id. TextView01, context. getResources ()

. GetString (R. string. load ));

 

} Else {

Rv. setTextViewText (R. id. TextView01, context. getResources ()

. GetString (R. string. change ));

 

}

Toast. makeText (context, Boolean. toString (uitil. isChange ),

Toast. LENGTH_LONG). show ();

Uitil. isChange =! Uitil. isChange;

 

}

AppWidgetManager appWidgetManger = AppWidgetManager

. GetInstance (context );

Int [] appIds = appWidgetManger. getAppWidgetIds (new ComponentName (

Context, widgetProvider. class ));

AppWidgetManger. updateAppWidget (appIds, rv );

}

 

Public static void updateAppWidget (Context context,

AppWidgetManager appWidgeManger, int appWidgetId ){

Rv = new RemoteViews (context. getPackageName (), R. layout. main );

Intent intentClick = new Intent (CLICK_NAME_ACTION );

PendingIntent pendingIntent = PendingIntent. getBroadcast (context, 0,

IntentClick, 0 );

Rv. setOnClickPendingIntent (R. id. TextView01, pendingIntent );

AppWidgeManger. updateAppWidget (appWidgetId, rv );

}

}

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.