Android Widget development details

Source: Internet
Author: User

This article focuses on the concept of Widget development. In this example, we want to develop a Widget on the mobile Android platform. The content of this Widget is obtained from the official website based on the input account. Of course, this process requires an API to process and display the information after obtaining the information. This is the general process. Now, proceed to the first step.
Android Widget Development Series (2)
This account is a test account, the user name is "students", and the password is "111111". Do not change it without authorization.
2. Create a Widget
The Androidreference contains detailed methods for creating a Widget. Here, we will briefly describe how to create a Widget. For more information, see the reference in AndroidSDK.
To create a Widget Development Program, perform the following steps:
(1) create a class that inherits the AppWidgetProvider class. There are many methods in AppWidgetProvider, such as onDelete (Context, int []) and onEnable (Context, but in general, we only override the onUpdate (Context, AppWidgetManager, int []) method. In this method, we start the background Service class, which is generally the Thread class or the Service class in Android. In this class, we obtain and process data from the server and display the data in the Widget.
(2) Add a receiver tag in your AndroidMenifest. xml to point it to your AppWidgetProvider subclass. The content is as follows:
<! --

Code highlighting produced by Actipro CodeHighlighter (freeware)
Http://www.CodeHighlighter.com/

--> 1. <receiverandroid: namereceiverandroid: name = "JiwaiWidget"
2. android: label = "@ string/app_name"
3. android: icon = "@ drawable/jiwai">
4. <intent-filter>
5. <actionandroid: nameactionandroid: name ="
Android. appwidget. action. APPWIDGET_UPDATE "/>
6. </intent-filter>
7. <meta-dataandroid: namemeta-dataandroid: name = "android. appwidget. provider"
8. android: resource = "@ xml/info"/>
9. </Cycler>
The above code is explained:
The first line specifies that the receiver of the Widget development is JiwaiWidget, that is, the AppWidgetProvider subclass you created;
The second row specifies the Tag Name of the Widget. The value is the app_name value in string. xml under the value directory;
The third row specifies the Widget development icon with the value of jiwai under the drawable directory;
Row 4-Row 6 is provided in the Android document;
Row 7 specifies the descriptive information of the Widget, which defines the relevant information of the Widget, such as its width, length, and automatic update interval, the description is located in info under the xml directory. in xml.
(3) Compile the provider file information of your Widget (xml/info. xml in this example)
<! --

Code highlighting produced by Actipro CodeHighlighter (freeware)
Http://www.CodeHighlighter.com/

--> <Appwidget-providerxmlns: androidappwidget-providerxmlns: android =
Http://schemas.android.com/apk/res/android"
Android: minWidth = "200dp"
Android: minHeight = "90dp"
Android: updatePeriodMillis = "43200000"
Android: initialLayout = "@ layout/appwidget"
Android: configure = "com. Alibaba enst. jiwai. JiwaiConfigure">
</Appwidget-provider>
Android: updatePeriodMillis is the time interval for automatic update, and android: initialLayout is the interface description file of the Widget. Android: configure is optional. If your Widget needs to start an Activity at startup, you need to set this item to your Activity. In this example, you need your account and password, so you should first display an Activity, enter your account and password, and then display the information in your Widget.
(4) Compile the appwidget. xml file in the layout directory to configure the interface information of your Widget:
<! --

Code highlighting produced by Actipro CodeHighlighter (freeware)
Http://www.CodeHighlighter.com/

--> Xmlversionxmlversion = "1.0" encoding = "UTF-8"?>
LinearLayoutxmlns: androidLinearLayoutxmlns: android =
Http://schemas.android.om/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: orientation = "vertical"
Android: id = "@ + id/widget"
Android: background = "@ drawable/title_a">
LinearLayoutandroid: layout_widthLinearLayoutandroid: layout_width = "fill_paren"
Android: orientation = "horizontal"
Android: layout_height = "wrap_content"
Android: background = "@ drawable/title">
<TextViewandroid: idTextViewandroid: id = "@ + id/username_display"
Android: textStyle = "bold"
Android: layout_width = "wrap_content"
Android: layout_height = "fill_parent"
Android: textColor = "# ffffff"
Android: textSize = "15px"
Android: gravity = "left | center_vertical"
Android: paddingLeft = "6px"/>
</LinearLayout>
 
<LinearLayoutandroid: orientationLinearLayoutandroid: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
 
<TextViewandroid: idTextViewandroid: id = "@ + id/text1"
Android: layout_width = "fill_parent"
Android: textColor = "# ffffff"
Android: textSize = "12px"
Android: gravity = "center_vertical | left"
Android: paddingLeft = "6px"
Android: layout_height = "30px">
</TextView>
 
<TextViewandroid: idTextViewandroid: id = "@ + id/text2"
Android: textColor = "# ffffff"
Android: layout_height = "30px"
Android: gravity = "center_vertical | left"
Android: textSize = "12px"
Android: paddingLeft = "6px"
Android: layout_width = "fill_parent">
</TextView>
</LinearLayout>
</LinearLayout>
The Widget contains three textviews, two for displaying information and one for displaying the user name. The above code is relatively simple, so it is not explained.
 
(5) because an acviyout object is required to input account information, create a login. xml file in the layout directory as the Activity configuration file:
<! --

Code highlighting produced by Actipro CodeHighlighter (freeware)
Http://www.CodeHighlighter.com/

--> <? Xmlversionxmlversion = "1.0" encoding = "UTF-8"?>
LinearLayoutxmlns: androidLinearLayoutxmlns: android =
Http://schemas.android.om/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<TextViewandroid: layout_widthTextViewandroid: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
Android: textColor = "# ff8c00"
Android: capitalize = "characters"
Android: textStyle = "bold"/>
 
<LinearLayoutandroid: orientationLinearLayoutandroid: orientation = "horizontal"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: gravity = "center_horizontal">
 
<TextViewandroid: layout_widthTextViewandroid: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/user"
Android: textColor = "# ff8cff"
Android: capitalize = "characters"/>
 
<EditTextandroid: idEditTextandroid: id = "@ + id/username"
Android: layout_width = "200px"
Android: layout_height = "wrap_content"/>
 
</LinearLayout>
 
<LinearLayoutandroid: orientationLinearLayoutandroid: orientation = "horizontal"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: gravity = "center_horizontal">
 
<TextViewandroid: layout_widthTextViewandroid: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/code"
Android: textColor = "# ff8cff"
Android: capitalize = "characters"/>
 
<EditTextandroid: idEditTextandroid: id = "@ + id/password"
Android: layout_width = "200px"
Android: layout_height = "wrap_content"
Android: password = "true"/>
</LinearLayout>
 
<LinearLayoutandroid: orientationLinearLayoutandroid: orientation = "horizontal"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: gravity = "center_horizontal">
 
<Button
Android: id = "@ + id/submit"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "Submit"
/>
</LinearLayout>
</LinearLayout>
There are two edittexts used to enter the user name and password, and a Button object.
The preparation is almost done. You can write the code below.

 

This article is from "Knowledge Ocean"
 

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.