Android mobile guard 14-Widget widget AppWidgetProvider, Widget add widget

Source: Internet
Author: User

Android mobile guard 14-Widget widget AppWidgetProvider, Widget add widget

 

1. AndroidManifest. xml broadcasts the receiver keyword android. appwidget. action. APPWIDGET_UPDATE based on the form widget.
Search for android: resource = "@ xml/process_widget_provider"


2. Find process_widget_provider.xml in the xml folder.

<appwidget-provider android:minWidth="294.0dip"     android:minHeight="72.0dip"     android:updatePeriodMillis="0"    android:initialLayout="@layout/process_widget"    xmlns:android="http://schemas.android.com/apk/res/android" />

 

3. Layout file process_widget pointed to by initial Layout

 

4. Analysis of the lifecycle of form widgets

1 public class MyAppWidgetProvider extends AppWidgetProvider {2 private static final String tag = "MyAppWidgetProvider"; 3 @ Override 4 public void onReceive (Context context, Intent intent) {5 Log. I (tag, "onReceive ............ "); 6 super. onReceive (context, intent); 7} 8 @ Override 9 public void onEnabled (Context context) {10 // method 11 Log for creating the first form widget. I (tag, "onEnabled creates the first form widget call method"); 12 // enable the Service (onCreate) 13 context. startService (new Intent (context, UpdateWidgetService. class); 14 super. onEnabled (context); 15} 16 @ Override17 public void onUpdate (Context context, AppWidgetManager appWidgetManager, 18 int [] appWidgetIds) {19 Log. I (tag, "onUpdate create one more form widget call method"); 20 // enable service 21 context. startService (new Intent (context, UpdateWidgetService. class); 22 super. onUpdate (context, appWidgetManager, appWidgetIds); 23} 24 @ Override25 public void onAppWidgetOptionsChanged (Context context, 26 AppWidgetManager appWidgetManager, int appWidgetId, 27 Bundle newOptions) {28 // call the method when the width and height of the form widget change. Call this method when creating the widget. 29 // enable the Service 30 context. startService (new Intent (context, UpdateWidgetService. class); 31 Log. I (tag, "onAppWidgetOptionsChanged create one more form widget call method"); 32 super. onAppWidgetOptionsChanged (context, appWidgetManager, appWidgetId, 33 newOptions); 34} 35 36 @ Override37 public void onDeleted (Context context, int [] appWidgetIds) {38 Log. I (tag, "onDeleted delete a form widget call method"); 39 super. onDeleted (context, appWidgetIds); 40} 41 42 @ Override43 public void onDisabled (Context context) {44 Log. I (tag, "onDisabled Delete the last form widget call method"); 45 // close service 46 context. stopService (new Intent (context, UpdateWidgetService. class); 47 super. onDisabled (context); 48} 49}
MyAppWidgetProvider

5. Update process count and available memory size of form widgets

1. Place the update process in the service. When will the service be enabled? When will the service be closed?

2. Once a form widget appears, you must enable the Service. When all form widgets are destroyed, disable the service.

1 public class UpdateWidgetService extends Service {2 protected static final String tag = "UpdateWidgetService"; 3 private Timer mTimer; 4 private InnerReceiver mInnerReceiver; 5 @ Override 6 public void onCreate () {7 // total management process count and available memory count Update (timer) 8 startTimer (); 9 10 // register to unlock broadcast recipient 11 IntentFilter intentFilter = new IntentFilter (); 12 // unlock action13 intentFilter. addAction (Intent. ACTION_SCREEN_ON); 14 // unlock acti On15 intentFilter. addAction (Intent. ACTION_SCREEN_OFF); 16 17 mInnerReceiver = new InnerReceiver (); 18 registerReceiver (mInnerReceiver, intentFilter); 19 20 super. onCreate (); 21} 22 23 class InnerReceiver extends BroadcastReceiver {24 @ Override25 public void onReceive (Context context, Intent intent) {26 if (intent. getAction (). equals (Intent. ACTION_SCREEN_ON) {27 // enable the Scheduled Update task 28 startTimer (); 29} else {30 // disable Update task 31 cancelTimerTask (); 32} 33} 34} 35 36 private void startTimer () {37 mTimer = new Timer (); 38 mTimer. scheduleAtFixedRate (new TimerTask () {39 @ Override40 public void run () {41 // The ui regularly refreshes 42 updateAppWidget (); 43 Log. I (tag, "a 5-second scheduled task is currently running .......... "); 44} 45}, 0, 5000); 46} 47 public void cancelTimerTask () {48 // The cancel Method in mTimer cancels the scheduled task method 49 if (mTimer! = Null) {50 mTimer. cancel (); 51 mTimer = null; 52} 53} 54 protected void updateAppWidget () {55 // 1. obtain the AppWidget object 56 AppWidgetManager aWM = AppWidgetManager. getInstance (this); 57 // 2. obtain the view object converted from the form widget layout (locate the package name of the application and the layout file in the current application). 58 RemoteViews remoteViews = new RemoteViews (getPackageName (), R. layout. process_widget); 59 // 3. assign 60 remoteViews to the internal control of the view object for the form widget. setTextViewText (R. id. TV _process_count, "Total number of processes:" + Process InfoProvider. getProcessCount (this); 61 // 4. display available memory size 62 String strAvailSpace = Formatter. formatFileSize (this, ProcessInfoProvider. getAvailSpace (this); 63 remoteViews. setTextViewText (R. id. TV _process_memory, "available memory:" + strAvailSpace); 64 65 66 // click the form widget to enter the application 67 // 1: Return to the click event on the Control 2: extended Intent 68 intent Intent = new Intent ("android. intent. action. HOME "); 69 intent. addCategory ("android. intent. category. DEFAULT "); 70 Pend IngIntent pendingIntent = PendingIntent. getActivity (this, 0, intent, PendingIntent. FLAG_CANCEL_CURRENT); 71 remoteViews. setOnClickPendingIntent (R. id. ll_root, pendingIntent); 72 73 // send a broadcast with an extended Intent, and kill the process in the broadcast receiver. for matching rules, see action74 Intent broadCastintent = new Intent ("android. intent. action. KILL_BACKGROUND_PROCESS "); 75 PendingIntent broadcast = PendingIntent. getBroadcast (this, 0, broadCastintent, PendingIntent. FL AG_CANCEL_CURRENT); 76 remoteViews. setOnClickPendingIntent (R. id. btn_clear, broadcast); 77 78 // context. The form widget corresponds to the byte code file 79 ComponentName componentName = new ComponentName (this, MyAppWidgetProvider. class); 80 // update the form widget 81 aWM. updateAppWidget (componentName, remoteViews); 82} 83 @ Override84 public IBinder onBind (Intent intent) {85 return null; 86} 87 @ Override88 public void onDestroy () {89 if (mInnerReceiver! = Null) {90 unregisterReceiver (mInnerReceiver); 91} 92 // call onDestroy to disable the service. The method to disable the service is called when the last form widget is removed, scheduled tasks do not need to maintain 93 cancelTimerTask (); 94 super. onDestroy (); 95} 96}
UpdateWidgetService

 

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.