Android gets started services from devices and determines whether a service is started.

Source: Internet
Author: User

During development, we often obtain the services started by the system to determine whether the server needs to be restarted.

This article describes the services started on android devices and determines whether a service is started.


1. Obtain system services based on ACTIVITY_SERVICE

ActivityManager = (ActivityManager) getSystemService (ACTIVITY_SERVICE );

2. obtain the services running in the system

RunningList = activityManager. getRunningServices (30); // represents the number of services we want to return 30

:


The instance code is as follows: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + MSkgYWN0aXZpdHlfbWFpbi54bWw8L3A + cjxwp1_vcd4kphbyzsbjbgfzcz0 = "brush: java;"> 2). strings. xml

 
     
  
   
AndroidServiceDemo
      
  
   
Show all services:
      
  
   
Settings
      
  
   
Retrieve all services
  
 
3) MainActivity. java
Package com. example. androidservicedemo; import java. util. list; import android. app. activity; import android. app. activityManager; import android. OS. bundle; import android. view. menu; import android. view. menuItem; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. textView; public class MainActivity extends Activity {private TextView displayAllServiceTv; private Button getAllServiceBtn; private ActivityManager activityManager; private List runningList; private String myServiceClassName = "org. linphone. linphoneService "; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); final String defaultStr = this. getResources (). getString (R. string. display_all_service); displayAllServiceTv = (TextView) this. findViewById (R. id. display_service_ TV); getAllServiceBtn = (Button) this. findViewById (R. id. service_btn); getAllServiceBtn. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stubStringBuffer displaySb = new StringBuffer (); displaySb. append (defaultStr ). append ("\ n"); activityManager = (ActivityManager) getSystemService (ACTIVITY_SERVICE); runningList = activityManager. getRunningServices (30); // represents the number of services we want to return. The value is 30displaySb. append (myServiceClassName ). append (":"). append (myServiceIsStart (runningList, myServiceClassName )). append ("\ n"); displayAllServiceTv. setText (getAllService (runningList, displaySb ). toString () ;}}) ;}private boolean myServiceIsStart (List runningList, String myServiceClassName) {boolean isStart = false; for (ActivityManager. runningServiceInfo runningService: runningList) {if (myServiceClassName. equals (runningService. service. getClassName () {isStart = true; break ;}} return isStart;} private StringBuffer getAllService (List runningList, StringBuffer displaySb) {for (ActivityManager. runningServiceInfo runningService: runningList) {displaySb. append (runningService. service. getClassName ()). append ("\ n");} return displaySb;} @ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}@ Overridepublic boolean onOptionsItemSelected (MenuItem item) {// Handle action bar item clicks here. the action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml.int id = item. getItemId (); if (id = R. id. action_settings) {return true;} return super. onOptionsItemSelected (item );}}



Related Article

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.