列出Android设备中所有启动的服务,及判断某个服务是否开启,具体步骤如下了:
第一步:新建一个Android工程,命名为RunningService。
第二步:修改RunningService.java代码如下:
view plaincopy to clipboardprint?
package com.tutor.runningservice;
import java.util.List;
import android.app.Activity;
import android.app.ActivityManager;
import android.os.Bundle;
import android.widget.TextView;
public class RunningService extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
TextView mTextView = new TextView(this);
ActivityManager mActivityManager =
(ActivityManager)getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> mServiceList = mActivityManager.getRunningServices(30);
//我要判断的服务名字,我在launcher2里加了一个音乐服务
final String musicClassName = "com.android.launcher2.MusicService";
boolean b = MusicServiceIsStart(mServiceList, musicClassName);
mTextView.setText("你要判断的服务状态为: " +b+"/n" + getServiceClassName(mServiceList));
setContentView(mTextView);
}
//通过Service的类名来判断是否启动某个服务
private boolean MusicServiceIsStart(List<ActivityManager.RunningServiceInfo> mServiceList,String className){
for(int i = 0; i < mServiceList.size(); i ++){
if(className.equals(mServiceList.get(i).service.getClassName())){
return true;
}
}
return false;
}
//获取所有启动的服务的类名
private String getServiceClassName(List<ActivityManager.RunningServiceInfo> mServiceList){
String res = "";
for(int i = 0; i < mServiceList.size(); i ++){
res+=mServiceList.get(i).service.getClassName()+ " /n";
}
return res;
}
}
package com.tutor.runningservice;
import java.util.List;
import android.app.Activity;
import android.app.ActivityManager;
import android.os.Bundle;
import android.widget.TextView;
public class RunningService extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
TextView mTextView = new TextView(this);
ActivityManager mActivityManager =
(ActivityManager)getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> mServiceList = mActivityManager.getRunningServices(30);
//我要判断的服务名字,我在launcher2里加了一个音乐服务
final String musicClassName = "com.android.launcher2.MusicService";
boolean b = MusicServiceIsStart(mServiceList, musicClassName);
mTextView.setText("你要判断的服务状态为: " +b+"/n" + getServiceClassName(mServiceList));
setContentView(mTextView);
}
//通过Service的类名来判断是否启动某个服务
private boolean MusicServiceIsStart(List<ActivityManager.RunningServiceInfo> mServiceList,String className){
for(int i = 0; i < mServiceList.size(); i ++){
if(className.equals(mServiceList.get(i).service.getClassName())){
return true;
}
}
return false;
}
//获取所有启动的服务的类名
private String getServiceClassName(List<ActivityManager.RunningServiceInfo> mServiceList){
String res = "";
for(int i = 0; i < mServiceList.size(); i ++){
res+=mServiceList.get(i).service.getClassName()+ " /n";
}
return res;
}
}
第三步:运行上述工程,查看效果!
不看不知道,一看吓一跳,乖乖,开了这么多服务,难怪手机很快就没电了。
上面的一些服务好多是系统自带的,并且不可见的,我们可以到Settings->Applications->Running services查看
一共才看到十来个服务。
OK,今天就这么多,呵呵,继续上班中..............
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Android_Tutor/archive/2010/08/19/5824581.aspx#