Complete parsing of Android phones

Source: Internet
Author: User
Tags home screen

With the release of the first Android phone, Android users began to write various application widgets. With the publication of the Widget API, developers are given a new and interesting development model. In addition to traditional telephone applications, developers can also develop other applications.

Developers can use the Widget API (included in the Android mobile phone, the latest version has reached Android 2.0) to create simple controls, and then display and use these controls in the new widgets. This article describes how to create a Widget on the home screen from scratch.

Use the AlarmManager interface to update images at user-defined intervals. You will see how to create a Widget and how to randomly select an image display from a group of images and periodically change the display image according to the user-defined time interval.

To create a simple Widget, follow these steps:

1. Create a RemoteView that provides a user interface for the Widget;

2. Bind RemoteView to an Activity (behavior) to implement the AppWidgetProvider interface;

3. Provide the key configuration information of the Widget in the manifest configuration file of the Android mobile phone.

A Widget for project preparation is a BroadcastReceiver that processes specific behaviors. The AppWidgetProvider interface provides a framework for developers to simplify the process of these behaviors. It includes the following methods:

1. onEnabled (): called when the first Widget is created. If yes, perform global initialization here.

2. onDisabled (): It is opposite to onEnabled (). It is called only when the last Widget is created. If yes, global cleaning should be performed here.

3. onUpdate (): it is called when the Widget needs to update its View. You also need to call it when creating the Widget for the first time.

4. onDeleted (): it is called when a specific instance of the Widget is deleted. It should be placed here for cleanup.

5. onReceive (): This method handles BroadcastReceiver behavior by default and calls the above method (warning: according to the relevant documentation, developers need to handle some special situations by themselves, for more information, see the following description ).

You may have noticed that, unlike common definitions, the section references an XML file resource, which defines additional data for the Widget. Consistent with the AppWidgetProviderInfo class, the information defined here remains unchanged. Therefore, this example does not include the updatePeriodMillis value.

This program allows users to modify and update the time. If you allocate updatePeriodMillis here, it cannot do this. The complete code of the imageswidget_info.xml file is as follows:

 
 
  1. <receiver android:name="ImagesWidgetProvider"> 
  2. 2    <intent-filter> 
  3. 3    <action 
  4. 4    android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
  5. 5    </intent-filter> 
  6. 6    <meta-data 
  7. 7    android:name="android.appwidget.provider" 
  8. 8    android:resource="@xml/imageswidget_info" /> 
  9. </receiver> 
  10. 10 

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.