当前位置: 编程技术>移动开发
在Android中 获取正在运行的Service 实例
来源: 互联网 发布时间:2014-10-14
本文导语: public class ServiceList extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); ActivityManager activityManger = (ActivityManager) getSystemService(ACTIVITY_SERVICE);// 获取Activity...
public class ServiceList extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this);
ActivityManager activityManger = (ActivityManager) getSystemService(ACTIVITY_SERVICE);// 获取Activity管理器 List serviceList = activityManger.getRunningServices(30);// 从窗口管理器中获取正在运行的Service tv.setText(getServicesName(serviceList)); setContentView(tv); } private boolean ServiceIsStart(List list, String className) {// 判断某个服务是否启动 for (int i = 0; i < list.size(); i++) { if (className.equals(list.get(i).service.getClassName())) return true; } return false; } private String getServicesName(List list) {// 获取所有服务的名称 String res = ""; for (int i = 0; i < list.size(); i++) { res += list.get(i).service.getClassName() + "/n"; } return res; } }