Android-app widget (3)

Source: Internet
Author: User

Use the appwidgetprovider class

The appwidgetprovider class inherits the broadcastreceiver class, which can conveniently process appwidget broadcasts. Appwidgetprovider only receives
Event broadcasts related to widgets, such as APP widgets updated, deleted, enabled, and disabled. When these broadcast events occur, appwidgetprovider receives the following method calls:

Onupdate ()

Call this method within the specified interval to update the app widget. The interval is defined by the updateperiodmillis attribute in appwidgetproviderinfo. This method is also called when a user adds an app widget. Therefore, it should perform basic installation, such as defining the event processor for view and starting a temporary service if needed. However, if you declare the activity for configuration, this method will not be called when you add an app widget, but subsequent updates will be called. After the configuration is complete, the activity configuration is responsible for executing the first update.

Onappwidgetoptionschanged ()

This method is called when the widget is placed for the first time and when the widget size is adjusted. You can use this callback method to display and hide content based on the widget size. Call the getappwidgetoptionss () method to obtain the widget size range. It returns a bundle object containing the following information:

1. option_appwidget_min_width --- use DP as the widget distance to control the lower limit of the current width.

2. option_appwidget_min_height --- use DP as the widget distance to control the lower limit of the current height.

3. option_appwidget_max_width --- use DP as the widget distance to control the upper limit of the current width.

4. option_appwidget_max_height --- use DP as the widget distance to control the upper limit of the current height.

This callback method is introduced in API level 16 (android4.1. If you implement this callback method, make sure that your app does not depend on it, because this method will not be called in earlier versions.

Ondeleted (context, int [])

This method is called every time the app widget is deleted from its owner.

Onenabled (context)

This method is called when an app widget instance is created for the first time. For example, if a user adds two identical app widget instances, this method is called only when the app widget instance is created for the first time. If you need to open a new database or perform one operation, it is better to use this callback method.

Ondisabled (context)

When you delete the last app widget from the app widget owner, this method is called. In this method, you should perform some cleanup operations, such as deleting a temporary database.

Onreceive (context, intent)

Each broadcast calls this method before the preceding callback method. Generally, you do not need to implement this method, because the default appwidgetprovider implements Filtering for all app widget broadcasts, the above callback method will be called as appropriate.

The most important appwidgetprovider callback method is onupdate (), because this method is called when the app widget is added to the owner (unless you have used a configuration activity. If your app widget receives some User Interaction Events, you need to use this callback method to register the event processor. If your app widget does not create temporary files, databases, or other necessary cleanup tasks, you can define only the onupdate () method. For example, if you want to click the app
To start an activity, you can use the following appwidgetprovider:

Publicclassexampleappwidgetproviderextendsappwidgetprovider {

Public void onupdate (context, appwidgetmanager, int [] appwidgetids ){
Final int n = appwidgetids. length;

// Define mThis loop procedure for each app widget that belongs to this provider
For (INT I = 0; I <n; I ++ ){
Int appwidgetid = appwidgetids [I];

// Create an intent to launch exampleactivity
Intent intent = new intent (context, exampleactivity. Class );
Pendingintent = pendingintent. getactivity (context, 0, intent, 0 );

// Get the layout for the app widget and attach an on-click listener
// To the button
Remoteviews views = new remoteviews (context. getpackagename (), R. layout. appwidget_provider_layout );
Views. setonclickpendingintent (R. Id. Button, pendingintent );

// Tell the appwidgetmanager to perform an update on the current appwidget
Appwidgetmanager. updateappwidget (appwidgetid, views );
}
}
}

This appwidget only defines the onupdate () method, starts to define a pendingintent object for starting the activity, and uses setonclickpendingintent (INT,
Pendingintent) method to bind it to the app widget button. Note that the onupdate () method uses a loop to traverse every entity in appwidgetids. appwidgetids contains every app created by the provider.
ID of the widget instance. In this way, if you create multiple app widget instances, all instance updates are executed simultaneously. However, there is only one updateperiodmillis schedule to manage all apps
Widget instance. For example, if the update plan is defined as every two hours and the second app widget instance is added one hour after the first one, both instances use the update cycle defined by the first instance, and the update cycle defined by the second instance is ignored.

Note: Because the appwidgetprovider class inherits the broadcastreceiver class, after the callback method returns, your processing is not guaranteed to be running (for information about the broadcast life cycle, see the broadcastreceiver class ). If you install your app
The widget process takes several seconds (probably because you want to execute a Web request), and you want your processing to maintain continuity, you need to consider onupdate () method To start a service. In this service, you can update the app widget without worrying about disabling appwidgetprovider because the app has no response (ANR) error. For the example of Running Service in APP widgets, see appwidgetprovider.

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.