Desktop widgets-Cleaning Process
First, let's briefly talk about What widgets are. widgets are the widgets added to the Android system interface. In android, a built-in system service is provided to manage Widgets. The service is AppWidgetManager.
To create a widget, follow these steps:
1. Create a project, write a class to inherit AppWidgetProvider, and override the onEnabled (), onUpdate (), onDisabled () methods.
Class TimeWidgetProvider extends appwikietprovider
{
}
2. appwietprovider is a broadcaster. Configure in the configuration file:
<Cycler android: name = ". cycler. MyWidget">
<Intent-filter>
<Action android: name = "android. appwidget. action. APPWIDGET_UPDATE"/>
</Intent-filter>
<Meta-data android: name = "android. appwidget. provider"
Android: resource = "@ xml/appwidget_info"/>
</Cycler>
3. Create an xml folder under the res folder and place an appwidget_info.xml file as a widget interface.
When the first widget is added on the table, the system sends a broadcast, which is received by our defined TimeWidgetProvider and executed the onEnabled () method, after the widget is added, a broadcast is sent. The broadcast type is:
Android. appwidget. action. APPWIDGET_UPDATE executes the onUpdate () method. When the last Widget of the same type is deleted on the desktop, the onDisabled () method is executed.
You can change the content on the widget interface in the onUpate () method.
As follows:
650) this. width = 650; "style =" width: 223px; height: 323px; "title =" widget.png "alt =" wKioL1LFMeOC4cTwAABf1obg3pU154.jpg "src =" http://www.bkjia.com/uploads/allimg/140104/005S43439-0.jpg "width =" 253 "height =" 371 "/>
When we click "one-click cleanup", some running processes on the mobile phone will be terminated. Of course, the user system cannot end before obtaining the root permission.
Step 1:
Write a class that inherits the AppWidgetProvider
Package cn. cbd. clean; import android. appwidget. appWidgetManager; import android. appwidget. appWidgetProvider; import android. content. context; import android. content. intent; public class CleanWidget extends AppWidgetProvider {private Intent service; @ Overridepublic void onDeleted (Context context, int [] appWidgetIds) {super. onDeleted (context, appWidgetIds) ;}@ Overridepublic void onDisabled (Context context) {// stop service = new Intent (context, MyService. class); context. stopService (service); super. onDisabled (context) ;}@ Overridepublic void onEnabled (Context context) {// enable service = new Intent (context, MyService. class); context. startService (service); super. onEnabled (context) ;}@ Overridepublic void onReceive (Context context, Intent intent) {super. onReceive (context, intent) ;}@ Overridepublic void onUpdate (Context context, AppWidgetManager appWidgetManager, int [] appWidgetIds) {// TODO Auto-generated method stubsuper. onUpdate (context, appWidgetManager, appWidgetIds );}}
AppWidgetProvider is also a broadcast receiver, so we need to configure CleanWidget in the configuration file
<Applicationandroid: icon = "@ drawable/ic_launcher" android: label = "@ string/app_name"> <er android: name = ". cleanWidget "> <intent-filter> <action android: name =" android. appwidget. action. APPWIDGET_UPDATE "/> </intent-filter> <! -- Meta-data used to transmit data --> <meta-dataandroid: name = "android. appwidget. provider "android: resource =" @ xml/example_appwidget_info "/> </receiver>
In the CleanWidget class, we can see that we enable a service in the ouEnabled () method and disable this service in onDisabled (). When we drag the widget to the desktop, the onReceive method will be executed first and then the onEnable () method will be executed. When we delete the same last control on the desktop, the onDisabled () method will be executed.
So what do we do in the service?
Package cn. cbd. clean; import java. util. arrayList; import java. util. list; import java. util. timer; import java. util. timerTask; import android. app. activityManager; import android. app. pendingIntent; import android. app. service; import android. app. activityManager. runningAppProcessInfo; import android. appwidget. appWidgetManager; import android. content. componentName; import android. content. context; import android. cont Ent. intent; import android. OS. IBinder; import android. text. format. formatter; import android. widget. remoteViews; public class MyService extends Service {private List <String> packageNames; private static final String BUTTON_KILLPROCESS = "cn. cbd. KILLPROCESS "; private Timer timer; @ Overridepublic void onCreate () {// create a timer to continuously monitor whether the number of programs running on the mobile phone changes Timer = new Timer (); timer. schedule (new TimerTask () {public void r Un () {// create a String collection to store the package name of the running program. We need to end the process through the package name of the application ). PackageNames = new ArrayList <String> (); // obtain the AppWidgetManager control administrator object AppWidgetManager appWidgetManager = AppWidgetManager. getInstance (getApplicationContext (); ActivityManager am = (ActivityManager) getApplicationContext (). getSystemService (Context. ACTIVITY_SERVICE); List <RunningAppProcessInfo> infos = am. getRunningAppProcesses (); for (RunningAppProcessInfo info: infos) {packageNames. add (info. processName);} // for this application, the desktop control is a remote View, so we need a RemoteViews object, to manage our custom ViewRemoteViews views = new RemoteViews (getApplicationContext (). getPackageName (), R. layout. memorycleanning); // sets the text content views on the control. setTextViewText (R. id. TV _runningAppNums, "running program has" + (packageNames. size () + "count"); views. setTextViewText (R. id. TV _availableMemory, "available memory" + Formatter. formatFileSize (getApplicationContext (), MemoryUtil. getAvailableMem (getApplicationContext (); // a custom broadcast with the type of cn is sent when you click the One-Click Clear button. cbd. in the KILLPROCESS configuration file, Intent intent = new Intent (); intent. setAction (BUTTON_KILLPROCESS); PendingIntent pendingIntent = PendingIntent. getBroadcast (getApplicationContext (), 0, intent, PendingIntent. FLAG_UPDATE_CURRENT); // Click Event views for the buttons on the widget. setOnClickPendingIntent (R. id. btn_clean, pendingIntent); ComponentName componentName = new ComponentName (getApplicationContext (), CleanWidget. class); // update AppAidget, that is, update the content of the desktop control appWidgetManager. updateAppWidget (componentName, views) ;}, 0, 10); super. onCreate () ;}@ Overridepublic IBinder onBind (Intent intent) {return null ;}@ Overridepublic void onDestroy () {super. onDestroy ();}}
Configuration of custom broadcast in the configuration file
<! -- Broadcast receiver custom broadcast --> <receiver android: name = ". killProcessBroadcaseReceiver "> <intent-filter> <action android: name =" cn. cbd. KILLPROCESS "/> </intent-filter> </Cycler>
At this point, we obviously want to write a broadcast receiver to receive this type of broadcast.
Package cn. cbd. clean; import java. util. arrayList; import java. util. list; import android. app. activityManager; import android. app. activityManager. runningAppProcessInfo; import android. appwidget. appWidgetManager; import android. content. broadcastReceiver; import android. content. componentName; import android. content. context; import android. content. intent; import android. text. format. formatter; import android. widget. remoteViews; public class extends BroadcastReceiver {private List <String> packageNames; @ Overridepublic void onReceive (Context context, Intent intent) {packageNames = new ArrayList <String> (); remoteViews views = new RemoteViews (context. getPackageName (), R. layout. memorycleanning); AppWidgetManager appWidgetManager = AppWidgetManager. getInstance (context); ActivityManager am = (ActivityManager) context. getSystemService (Context. ACTIVITY_SERVICE); List <RunningAppProcessInfo> infos = am. getRunningAppProcesses (); for (RunningAppProcessInfo info: infos) {packageNames. add (info. processName);} String action = intent. getAction (); if (action. equals ("cn. cbd. KILLPROCESS ") {for (String packageName: packageNames) {am. killBackgroundProcesses (packageName);} ComponentName componentName = new ComponentName (context, CleanWidget. class); views. setTextViewText (R. id. TV _runningAppNums, "running programs" + packageNames. size () + "count"); views. setTextViewText (R. id. TV _availableMemory, "available memory" + Formatter. formatFileSize (context, MemoryUtil. getAvailableMem (context); appWidgetManager. updateAppWidget (componentName, views );}}}
In this broadcast, we will use a tool class to obtain the remaining memory size of the mobile phone RAM. This class is our custom tool class.
Package cn. cbd. clean; import android. app. activityManager; import android. app. activityManager. memoryInfo; import android. content. context;/*** tool class, used to obtain the remaining memory size of the mobile phone * @ author Administrator **/public class MemoryUtil {// Formatter. formatFileSize (this, availaleMemory) format public static long getAvailableMem (Context context) {ActivityManager am = (ActivityManager) context. getSystemService (Context. ACTIVITY_SERVICE); MemoryInfo outInfo = new MemoryInfo (); am. getMemoryInfo (outInfo); return outInfo. availMem ;}}
At this point, our cell phone Cleaning Process widget has been completed. Now, let me sort out the ideas for this small project:
1. Get a Widget object inherited from AppWidgetProvider)
2. Enable a service to update the data changes on the control.
3. Define a broadcast in the service. When we click "one-click cleaning", a broadcast will be sent. At this time, we need to write a broadcast receiver to receive this type of broadcast we define.
4. In the broadcast, we killed the process.
This article is from the "SEVEN" blog, please be sure to keep this source http://930307.blog.51cto.com/7950022/1347793