Android-app widget (4)

Source: Internet
Author: User
Tags home screen

Intent object for receiving app widget Broadcast

Appwidgetprovider is just a convenient class. If you want to directly receive app widget broadcasts, You can implement your own broadcastreceiver class or override the onreceive (context, intent) callback method. You need to care about the following types of intent objects:

Action_appwidget_update

Action_appwidget_deleted

Action_appwidget_enabled

Action_appwidget_disabled

Action_appwidget_options_changed

Create a configuration activity for the appwidget

If you want to complete some settings when adding a new app widget, you can create an activity for configuration. This activity is automatically started by the app widget owner and allows users to make some effective settings for the app widget during creation, such as the color, size, update cycle, and other functional settings of the app widget.

This configuration activity is declared as a normal activity in the android list file. However, it will be started by the app widget using the action_appwidget_configure operation. Therefore, this activity needs to accept this intent object. For example:

<Activityandroid: Name = ". exampleappwidgetconfigure">
<Intent-filter>
<Action Android: Name = "android. appwidget. Action. appwidget_configure"/>
</Intent-filter>
</Activity>

Also, this activity must be declared using the Android: Configure attribute in the XML file of appwidgetproviderinfo. For example:

<Appwidget-providerxmlns: Android = "http://schemas.android.com/apk/res/android"
...
Android: Configure = "com. example. Android. exampleappwidgetconfigure"

...>
</Appwidget-provider>

Note that this activity should be declared using a complete namespace because it will be referenced outside the scope of your package.

The above is all you need to do to start the activity with configuration. The actual activity is required now. But remember two important things when implementing your activity:

1. The app widget owner will call this configuration activity and the configuration activity will always return results. This result should include the ID of the app widget (which is saved in the Additional Information of the intent object and key name: extra_appwidget_id ).

2.
When an appwidget is created, the onupdate () method is not called (because the system does not send an action_appwidget_update broadcast when the activity is configured to be started ). When the app
When a widget is created for the first time, this configuration activity requests updates from appwidgetmanager. However, the onupdate () method will be called by subsequent updates-it is skipped for the first time.

The following sections show the returned results from the configuration activity and the code snippet for updating the app widget.

Update app widgets from the configuration activity

When an app widget uses a configuration activity, it is responsible for updating the app widget after the configuration is complete. Send a request directly to appwidgetmanager to complete the update.

The following is a brief process of updating the app widget and disabling activity Configuration:

1. Retrieve app widgetid from the intent that starts the activity:

Intent intent
= Getintent ();
Bundle extras = intent. getextras ();
If (extras! = NULL ){
Mappwidgetid = extras. getint (
Appwidgetmanager. extra_appwidget_id,
Appwidgetmanager. invalid_appwidget_id );
}

2. Execute app widget Configuration

3. When the configuration is complete, call the getinstance (context) method to obtain an appwidgetmanager instance:

Appwidgetmanagerappwidgetmanager
= Appwidgetmanager. getinstance (context );

4. Call the updateappwidget (INT, remoteviews) method to update the app with the remoteviews layout.
Widget:

Remoteviews views
= Newremoteviews (context. getpackagename (),
R. layout. example_appwidget );
Appwidgetmanager. updateappwidget (mappwidgetid, views );

5. Finally, create an intent object to return, set the returned result, and destroy the current activity:

Intent resultvalue
= Newintent ();
Resultvalue. putextra (appwidgetmanager. extra_appwidget_id, mappwidgetid );
Setresult (result_ OK, resultvalue );
Finish ();

Tip: when your configuration activity is opened for the first time, set the activity setting result to result_canceled. If the user exits the activity before the setting ends, this method will notify the owner of the app widget that the configuration is canceled and the app widget will not be added.

 

Set preview Image

The previewimage field is introduced in android3.0 and later. It is used to specify the preview image of the appwidget and will be displayed in the widget selector. If this field is not supported, the image of the app widget is used for preview.

The following describes how to specify this setting in XML:

<Appwidget-providerxmlns: Android = "http://schemas.android.com/apk/res/android"
...
Android: previewimage = "@ drawable/preview">
</Appwidget-provider>

To help you create preview images for app widgets, the android simulator contains
Preview application. Start this application, select an app widget for your application, and create a preview image for it. Then, you can create a preview image for the app widget and save it, and put it in the deployable resources of your application.

Enable app widget on screen lock

Android4.2 introduced the ability to add widgets to the screen lock. To specify that your app widget is valid on the screen lock, declare the Android: widgetcategory attribute in appwidgetproviderinfo in the XML file. This attribute supports two values: "home_scree" and "keyguard ", one app
Widgets support one or both of them.

By default, each app widget can be placed on the home screen. Therefore, "home_screen" is the default value of the Android: widgetcategory attribute. If you want the app widget to be valid on the screen lock, you need to add the "keyguard" attribute value:

<Appwidget-providerxmlns: Android = "http://schemas.android.com/apk/res/android"
...
Android: widgetcategory = "keyguard | home_screen">
</Appwidget-provider>

If you declare that a widget will be displayed on both the screen lock and the home screen, you will want to customize the widget's appearance Based on the display position. For example, you may want to create an independent layout file for the screen lock and the home screen respectively. Next, check the Display category of the widget during running. Call the getappwidgetoptions () method to obtain a bundle object. You can check whether your widget is displayed on the screen lock or the home screen. The returned bundle object will contain option_appwidget_host_category, and its value will be either widget_category_home_screen or widget_appwidget_host_category. This value is used to determine the owner of the widget. In appwidgetprovider, you can check the type of the widget, for example:

Appwidgetmanager;
Int widgetid;
Bundle myoptions = appwidgetmanager. getappwidgetoptions (widgetid );

// Get the value of option_appwidget_host_category
Int Category = myoptions. getint (appwidgetmanager. option_appwidget_host_category,-1 );

// If the value is widget_category_keyguard, It's A lockscreenwidget
Boolean iskeyguard = Category = appwidgetproviderinfo. widget_category_keyguard;

Once you know the types of widgets, you can choose to load different la S and set different properties, such:

Int baselayout
= Iskeyguard
? R. layout. keyguard_widget_layout:
R. layout. widget_layout;

You can also use the Android: initalkeyguardlayout attribute to specify the initial layout of your app widget on the screen lock. This property works in the same way as the Android: initiallayout property. It provides a layout that can be displayed immediately until the app
The widget is initialized and can update the layout.

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.