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: