Android obtains the background Running services Get Running Service, androidrunning
The complete code can be seen on my GitHub. The link is at the end of the article. :
Main:
Package com. zms. getrunningservice; import java. util. arrayList; import java. util. collections; import java. util. comparator; import java. util. list; import android. app. activity; import android. app. activityManager; import android. app. alertDialog; import android. app. dialog; import android. content. componentName; import android. content. context; import android. content. dialogInterface; import android. content. intent; impo Rt android. content. pm. applicationInfo; import android. content. pm. packageManager; import android. content. pm. packageManager. nameNotFoundException; import android. OS. bundle; import android. OS. debug; import android. util. log; import android. view. contextMenu; import android. view. menu; import android. view. menuItem; import android. view. view; import android. view. contextMenu. contextMenuInfo; import android. widget. adap TerView; import android. widget. listView; import android. widget. textView; import android. widget. adapterView. onItemClickListener; public class Main extends Activity implements OnItemClickListener {private static String TAG = "RunningService"; private ActivityManager mActivityManager = null; // The ProcessInfo Model class is used to save all process information. private List <ServiceModel> serviceModelList = null; private ListView serviceList; Private TextView tvTotalServiceNo; public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. service_list); serviceList = (ListView) findViewById (R. id. listviewService); serviceList. setOnItemClickListener (this); tvTotalServiceNo = (TextView) findViewById (R. id. tvTotalServiceNo); // get the ActivityManager service object mActivityManager = (ActivityManager) getSy StemService (Context. ACTIVITY_SERVICE); // get the running Service information getRunningServiceInfo (); // sort the Collections. sort (serviceModelList, new comparatorServiceLabel (); // construct the adapter object ServiceListAdapter mServiceInfoAdapter = new ServiceListAdapter (Main. this, serviceModelList); serviceList. setAdapter (mServiceInfoAdapter); tvTotalServiceNo. setText (serviceModelList. size () + "services are running. Click" stop. ") ;}// Obtain the information about the processes that are running by the system. private void getRunningServiceInfo () {// set the default Service quantity. int defanum num = 200; // call the getRunningAppServicees () method of ActivityManager to obtain the List of all running processes in the System <ActivityManager. runningServiceInfo> runServiceList = mActivityManager. getRunningServices (defaultNum); // ServiceInfo Model class is used to save all process information serviceModelList = new ArrayList <ServiceModel> (); for (ActivityManager. runningServiceInfo RunServiceInfo: runServiceList) {// obtain the information of the process where the Service is located. int pid = runServiceInfo. pid; // ID of the process where the service is located: int uid = runServiceInfo. uid; // the user ID is similar to the Linux permission, and the ID is also different, such as root. // process name. The default value is package name or String processName = runServiceInfo specified by the attribute android: process. process; // The time when the Service is started long activeSince = runServiceInfo. activeSince; // if the Service is connected using the Bind method, clientCount indicates the number of clients that the service connects to. int clientCount = runSe RviceInfo. clientCount; // The component information of the Service may be pkgname/servicename ComponentName serviceCMP = runServiceInfo. service; String serviceName = serviceCMP. getaskclassname (); // service class name String pkgName = serviceCMP. getPackageName (); // package name // print Log. I (TAG, "process id:" + pid + "process name:" + processName + "process uid: "+ uid +" \ n "+" service Start Time Value: "+ activeSince +" number of client bindings: "+ clientCount +" \ n "+" This s Ervice component information: "+ serviceName +" and "+ pkgName); // here, we use the service component information to obtain the package name of the application where the service is located using PackageManager, packageManager mPackageManager = this. getPackageManager (); // obtain the PackagerManager object; try {// obtain the information of this pkgName ApplicationInfo appInfo = mPackageManager. getApplicationInfo (pkgName, 0); ServiceModel serviceModel = new ServiceModel (); serviceModel. setAppIcon (appInfo. loadIcon (mPackageManager )); ServiceModel. setAppLabel (appInfo. loadLabel (mPackageManager) + ""); serviceModel. setServiceName (serviceName); serviceModel. setPkgName (pkgName); // sets the service's component information Intent intent = new Intent (); intent. setComponent (serviceCMP); serviceModel. setIntent (intent); serviceModel. setPid (pid); serviceModel. setProcessName (processName); // Add it to serviceModelList in the set. add (serviceModel);} catch (NameNotFoundExcept Ion e) {// TODO Auto-generated catch block e. printStackTrace () ;}}// you can stop @ Override public void onItemClick (AdapterView <?> Arg0, View arg1, int position, long arg3) {// TODO Auto-generated method stub final Intent stopServiceIntent = serviceModelList. get (position ). getIntent (); new AlertDialog. builder (Main. this ). setTitle ("Stop Service "). setMessage ("the service can continue to run only after it is restarted. However, this may bring unexpected results to e-market applications. "). SetPositiveButton ("stop", new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {// TODO Auto-generated method stub // stop the Service due to insufficient permissions, to avoid exceptions in the application, capture the SecurityException and pop up the try {stopService (stopServiceIntent);} catch (SecurityException sEx) {// if an exception occurs, the System is not authorized enough. out. println ("deny the permission"); new AlertDialog. builder (Main. this ). setTitle ("insufficient Permissions "). setMessage ("sorry, you are not authorized enough to stop this Service "). create (). show () ;}// refresh the interface // get the running Service information getRunningServiceInfo (); // sort the Collections. sort (serviceModelList, new comparatorServiceLabel (); // create an adapter object ServiceListAdapter serviceListAdapter = new ServiceListAdapter (Main. this, serviceModelList); serviceList. setAdapter (serviceListAdapter); tvTotalServiceNo. setText ("currently running services:" + serviceModelList. size ());}}). setNegativeButton ("cancel", new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {// TODO Auto-generated method stub dialog. dismiss (); // cancel dialog box }}). create (). show () ;}// custom sorting private class comparatorServiceLabel implements Comparator <ServiceModel >{@ Override public int compare (ServiceModel object1, ServiceModel object2) by AppLabel) {// TODO Auto-generated method stub return object1.getAppLabel (). compareTo (object2.getAppLabel ());}}}
Reprinted please indicate the source: Zhou mu Shui CSDN blog http://blog.csdn.net/zhoumushui
My GitHub: Zhou mu Shui's GitHub https://github.com/zhoumushui