首页 > 代码库 > android 获取设备中已启动的服务并判断某一服务是否启动

android 获取设备中已启动的服务并判断某一服务是否启动

我们常常在开发的时候,通过获取系统已启动的服务来判断该服务器是否还需要再启动。

而本文将介绍android设备中已启动的服务,并判断某一服务是启动


1.根据ACTIVITY_SERVICE获取系统服务

activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);

2.获取系统中正在运行的服务

runningList = activityManager.getRunningServices(30);//代表我们希望返回的服务数目大小 30

效果图:


实例代码如下

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:orientation="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"
        android:layout_marginTop="20dp"
        android:text="@string/display_all_service" 
        android:textColor="#FF0000" />

</LinearLayout></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">显示所有服务:</string>
    <string name="action_settings">Settings</string>
    <string name="get_all_service">获取所有服务</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";
	@Override
	protected 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(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				StringBuffer displaySb = new StringBuffer();
				displaySb.append(defaultStr).append("\n");
				activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
				runningList = activityManager.getRunningServices(30);//代表我们希望返回的服务数目大小 30
				
				displaySb.append(myServiceClassName).append(":").append(myServiceIsStart(runningList,myServiceClassName)).append("\n");
				
				displayAllServiceTv.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;
	}

	@Override
	public 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;
	}

	@Override
	public 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);
	}

}
</span>