Android Widget (1) Simple implementation

Source: Internet
Author: User

Android Widget (1) Simple implementation

Add a Widget to the screen: Long and press the blank area on the screen, or find the selection of the WidgetPreview App. Native systems use a long press mode below 4.0, and open WIDGETS at 4.0 or above

General steps for creating a Widget:
In menifest

              
                                    
                                 
               
                   
  
 

Widget description file: timer_widget_provider.xml

 
 

Widget layout: timer_widget.xml
 
     
  
  
 

AppWidgetProvider for processing Widget-related events
Package com. stone. ui; import java. util. arrays; import com. stone. r; import com. stone. service. timerWidgetService; import android. app. pendingIntent; import android. appwidget. appWidgetHost; import android. appwidget. appWidgetManager; import android. appwidget. appWidgetProvider; import android. content. componentName; import android. content. context; import android. content. intent; import android. text. format. dateUtils; import android. widget. remoteViews;/** AppWidgetProvider must receive android. appwidget. action. APPWIDGET_UPDATE broadcast **/public class TimerWidgetProvider extends AppWidgetProvider {@ Override // called when updating parts. public void onUpdate (Context context, AppWidgetManager appWidgetManager, int [] appWidgetIds) {super. onUpdate (context, appWidgetManager, appWidgetIds); System. out. println ("onUpdate widget:" + Arrays. toString (appWidgetIds);/** construct a pendingIntent broadcast. onReceive () Updates */Intent intent = new Intent (context, TimerWidgetService Based on the received broadcast. class); PendingIntent pendingIntent = PendingIntent. getService (context, 0, intent, 0); RemoteViews rv = new RemoteViews (context. getPackageName (), R. layout. timer_widget); rv. setOnClickPendingIntent (R. id. start_stop, pendingIntent); appWidgetManager. updateAppWidget (appWidgetIds, rv) ;}@ Override // The widget deletes public void onDeleted (Context context, int [] appWidgetIds) {super. onDeleted (context, appWidgetIds); System. out. println ("onDeleted widget");} @ Override // This is called for the first time you create the widget. Otherwise, the public void onEnabled (Context context) {super. onEnabled (context); System. out. println ("onEnabled widget") ;}@ Override // call the public void onDisabled (Context context) {super. onDisabled (context); System. out. println ("onDisabled widget");} @ Override // public void onReceive (Context context, Intent intent) {super. onReceive (context, intent); System. out. println ("onReceive widget");/** receives these broadcasts from other components, activities, or services */if (intent. getAction (). equals ("com. stone. action. start ") {long time = intent. getLongExtra ("time", 0); updateWidget (context, time); System. out. println ("receive com. stone. action. start ") ;}} private void updateWidget (Context context, long time) {// RemoteViews process ViewRemoteViews rv = new RemoteViews (context. getPackageName (), R. layout. timer_widget); System. out. println ("time =" + time); rv. setTextViewText (R. id. counter, DateUtils. formatElapsedTime (time/1000); AppWidgetManager am = AppWidgetManager. getInstance (context); int [] appWidgetIds = am. getAppWidgetIds (new ComponentName (context, TimerWidgetProvider. class); am. updateAppWidget (appWidgetIds, rv); // update all instances }}

TimerWidgetService used in AppWidgetProvider

Package com. stone. service; import android. app. service; import android. content. intent; import android. OS. IBinder; public class TimerWidgetService extends Service {@ Overridepublic IBinder onBind (Intent intent) {return null ;}@ Overridepublic void onCreate () {super. onCreate () ;}@ Overridepublic int onStartCommand (Intent intent, int flags, int startId) {/** do some thing * // send a broadcast notification to the widget to update the sendBroadcast status (new Intent ("com. stone. action. start "). putExtra ("time", System. currentTimeMillis (); return Service. START_STICKY ;}}


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.