Android-based widget development example 1

Source: Internet
Author: User

Android widget DevelopmentCase implementation is the content to be introduced in this article, mainly to understand and learnAndroid
Widget Development
Application. Let's write about it today.Android widgetOfDevelopmentBecause it's time to wait early in the morning, I won't say it's too specific. Let's just imitate it, comrades! First, let's take a look:



 

The detailed steps of the demo are as follows:

1. Create an android project named widgetdemo.

2. Prepare materials. One is the widget icon and the other is the background of the widget. Storage directory:

3. Modify the string. xml file as follows:

<?xml version="1.0" encoding="utf-8"?>    <resources>        <string name="hello">Hello World, WidetDemo!</string>        <string name="app_name">DaysToWorldCup</string>    </resources>   

Iv. EstablishmentWidgetIn the content provider file, we create an XML folder under Res and create a new widget_provider.xml code:

<?xml version="1.0" encoding="utf-8"?>    <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"        android:minWidth="50dip"        android:minHeight="50dip"        android:updatePeriodMillis="10000"        android:initialLayout="@layout/main"/>    

The width and length are clear, and Android: updateperiodmillis is the time interval for automatic update, Android: initiallayout is the interface description file of the widget.

There is also an attribute Android: Configure is optional. If your widget needs to start an activity at startup, you need to set this item to your activity.

5. Modify the main. xml layout. The Code is as follows:

<?xml version="1.0" encoding="utf-8"?>    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"        android:orientation="vertical"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:background="@drawable/wordcup"        >    <TextView          android:id="@+id/wordcup"        android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="@string/hello"        android:textSize="12px"        android:textColor="#ff0000"        />    </LinearLayout>

6. modify the code of widgetdemo. Java as follows:

Package COM. android. tutor; import Java. util. calendar; import Java. util. date; import Java. util. gregoriancalendar; import Java. util. timer; import Java. util. timertask; import android. appwidget. appwidgetmanager; import android. appwidget. appwidgetprovider; import android. content. componentname; import android. content. context; import android. widget. remoteviews; public class widetdemo extends appwidgetprovider {/** called when the activity is first created. * // @ override public void onupdate (context, appwidgetmanager, int [] appwidgetids) {timer = new timer (); timer. scheduleatfixedrate (New mytime (context, appwidgetmanager), 1, 60000); super. onupdate (context, appwidgetmanager, appwidgetids);} private class mytime extends timertask {remoteviews; appwidgetmanager; componentname thiswidget; Public mytime (context, appwidgetmanager) {This. appwidgetmanager = appwidgetmanager; remoteviews = new remoteviews (context. getpackagename (), R. layout. main); thiswidget = new componentname (context, widetdemo. class) ;}public void run () {date = new date (); Calendar calendar = new gregoriancalendar (2010,06, 11); long days = (calendar. gettimeinmillis ()-date. gettime ()/1000)/86400; remoteviews. settextviewtext (R. id. wordcup, "+ days +" days "); appwidgetmanager. updateappwidget (thiswidget, remoteviews );}}}

7. modify the configuration file androidmanifest. xml. The Code is as follows:

<?xml version="1.0" encoding="utf-8"?>    <manifest xmlns:android="http://schemas.android.com/apk/res/android"          package="com.android.tutor"          android:versionCode="1"          android:versionName="1.0">        <application android:icon="@drawable/icon" android:label="@string/app_name">            <receiver android:name=".WidetDemo"                      android:label="@string/app_name">                <intent-filter>                    <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />                </intent-filter>                <meta-data android:name="android.appwidget.provider"                           android:resource="@xml/widget_provider"/>            </receiver>        </application>        <uses-sdk android:minSdkVersion="7" />    </manifest>    

Where

<receiver android:name=".WidetDemo"    android:label="@string/app_name">    

Name indicates that the receiver of the widget is widetdemo, that is, the appwidgetprovider subclass you created. The label specifies the tag of the widget and the icon can be specified using the attribute icon.

<intent-filter>    <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />   </intent-filter>  

Uses the intent provided in the android document for receiving updates

<meta-data android:name="android.appwidget.provider"  android:resource="@xml/widget_provider"/> 

Resource specifies the description of the widget. The description defines the information about the widget, such as its width, length, and automatic update interval, that is, the content defined in the previous four

8. Click "run" (CTRL + F11). After the operation is successful, we click "desktop" for a long time. The following two items are displayed. Click them one by one to see the top:

 

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.