android 上手维修设备和推断启动服务

android 上手维修设备和推断启动服务

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

下载链接:http://download.csdn.net/detail/a123demi/7511823

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

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


1.依据ACTIVITY_SERVICE获取系统服务

activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);

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

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

效果图:

android 上手维修设备和推断启动服务

实例代码例如以下

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>



版权声明:本文博主原创文章。博客,未经同意不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/116944.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号