Android Combat Simple Tutorial-the 68th gun (Android gadget appwidget time display)

Source: Internet
Author: User

Android Appwidget development differs from normal Android apps because Appwidget is a program that runs in other processes. It uses Remoteviews to update the UI. Once the system has changed, it is easy to cause appwidget update. It has a limited number of supported components and very few event types. Therefore, it is generally used for the components of the desktop display, which have a long update period and relatively simple events. Its development process is relatively simple. Broadly divided into:

1: Writing the layout file

<?xml version= "1.0" encoding= "Utf-8"? ><textview xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:id=" @+id/textview01 "    android:layout_width=" fill_parent "    android:layout_height=" Fill_parent "    android:gravity=" center "    android:text=" Starting ... "    android:textcolor=" #FFFF00 "    Android:textsize= "14dip"/>

2: Writing provider configuration files and manifest configuration files

<manifest xmlns:android= "http://schemas.android.com/apk/res/android"    package= "Com.mywidget"    android: Versioncode= "1"    android:versionname= "1.0" >    <uses-sdk        android:minsdkversion= "8"        android: targetsdkversion= "/>    <application        android:icon=" @drawable/ic_launcher "android:label="        @ String/app_name "        android:theme=" @style/apptheme ">        <receiver android:name=" MyTime "android:label=" MyTime ">            <intent-filter>                <action android:name=" Android.appwidget.action.APPWIDGET_UPDATE " />            </intent-filter>            <meta-data android:name= "Android.appwidget.provider" android:resource= "@xml/my_time"/>        </receiver>         <service android:name= "Mytime$myservice"/>    </ Application></manifest>

3: Writing business logic

Package Com.mywidget;import Java.util.date;import Java.util.timer;import java.util.timertask;import Android.os.handler;import Android.os.ibinder;import Android.os.message;import Android.app.Service;import Android.appwidget.appwidgetmanager;import Android.appwidget.appwidgetprovider;import Android.content.componentname;import Android.content.context;import Android.content.intent;import Android.widget.remoteviews;public class MyTime extends Appwidgetprovider {timer timer; Context context;//Onupdate@overridepublic void onUpdate (context con, Appwidgetmanager appwidgetmanager,int[] Appwidgetids) {context = Con;timer = new timer (); Timer.schedule (timertask, 0, 1000);//1 seconds call}//MyService Service Program Public Static class MyService extends Service {@Overridepublic void OnStart (Intent Intent, int startid) {remoteviews remoteviews = New Remoteviews (Getpackagename (), r.layout.my_time);//Set Display control Remoteviews.settextviewtext (r.id.textview01,new Date (). toLocaleString ()); ComponentName thiswidget = new ComponentnamE (this, mytime.class); Appwidgetmanager Manager = Appwidgetmanager.getinstance (this); Manager.updateappwidget (Thiswidget, remoteViews);} @Overridepublic ibinder onbind (Intent Intent) {return null;}};/ /handlerprivate Handler Handler = new Handler () {public void Handlemessage (Message msg) {Intent Intent = new Intent (conte XT, Myservice.class); Context.startservice (intent);}}; Private TimerTask TimerTask = new TimerTask () {public void run () {Message message = new Message (); Handler.sendmessage (mess age);}};}

Run the following example:


Favorite friends pay attention to me!

SOURCE download


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Combat Simple Tutorial-the 68th gun (Android gadget appwidget time display)

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.