Simple tutorial for Android-68th gun (time display of appwidget for android)

Source: Internet
Author: User

Simple tutorial for Android-68th gun (time display of appwidget for android)

Android AppWidget development is different from ordinary android applications, because AppWidget is a program running in other processes. It uses RemoteViews to update the UI. Once the system changes, AppWidget updates are easily triggered. Its supported components are limited, and there are few event types. Therefore, it is generally used for components with long update cycles and simple events for desktop display. The development process is relatively simple. It can be roughly divided:

1: Write layout files

 

 
 

 

2. Compile the Provider configuration file and Manifest configuration file.

 

     
              
               
                                
                
            
    
   
   
             
         
     
    
   
   
    
     
    
   
  
 

 

3: Write 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); // one-second 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); // sets the 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 (context, MyService. class); context. startService (intent) ;}}; private TimerTask timertask = new TimerTask () {public void run () {Message message = new Message (); handler. sendMessage (message );}};}

The running instance is as follows:

 

 

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.