Android Gets the service started in the device and determines whether a service is started

Source: Internet
Author: User

We often do this by getting the services that the system has started to determine if the server needs to be restarted.

This article describes the services that are started in the Android device and determines if a service is started


1. Access to system services according to Activity_service

Activitymanager = (Activitymanager) getsystemservice (Activity_service);

2. Get the service running in the system

Runninglist = activitymanager.getrunningservices (30);//Represents the number of services we want to return 30


The instance code is as follows

1) activity_main.xml

<span style= "FONT-SIZE:18PX;" ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:orien tation= "vertical" android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin "android:paddingright=" @dimen/activity_horizontal_margin "android:paddingtop=" @dimen/activity_        Vertical_margin "> <button android:id=" @+id/service_btn "android:layout_width=" Fill_parent " android:layout_height= "Wrap_content" android:text= "@string/get_all_service"/> <textview android:id = "@+id/display_service_tv" android:layout_width= "fill_parent" android:layout_height= "Fill_parent" and roid:layout_margintop= "20DP" android:text= "@string/display_all_service" android:textcolor= "#FF0000"/&GT;&L t;/linearlayout>&Lt;/span> 
2). Strings.xml
<span style= "FONT-SIZE:18PX;" ><?xml version= "1.0" encoding= "Utf-8"?><resources> <string    name= "App_name" > androidservicedemo</string>    <string name= "Display_all_service" > Show All Services:</string>    < String Name= "Action_settings" >Settings</string>    <string name= "Get_all_service" > Get all Services </ String></resources></span>
3) Mainactivity.java
<span style= "FONT-SIZE:18PX;" >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<activitymanager.runningserviceinfo > 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);d Isplayallservicetv = (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 ();d isplaysb.append (DEFAULTSTR). Append ("\ n"); Activitymanager = (Activitymanager) getsystemservice (activity_service); runninglist = Activitymanager.getrunningservices (30);//Represents the number of services we want to return 30displaysb.append (Myserviceclassname). Append (":"). Append (Myserviceisstart (Runninglist,myserviceclassname)). Append ("\ n");d Isplayallservicetv.settext ( Getallservice (RUNNINGLIST,DISPLAYSB). toString ());}}); Private Boolean Myserviceisstart (list<activitymanager.runningserviceinfo> 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<activitymanager.runningserviceinfo> 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 PR Esent.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 and RoidManifest.xml.int id = item.getitemid (); if (id = = r.id.action_settings) {return true;} return super.onoptionsitemselected (item);}} </span>



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.