在Android中 获取正在运行的Service 实例
程序员文章站
2023-12-11 22:57:10
public class servicelist extends activity {
@override
protected void oncreate(bu...
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<activitymanager.runningserviceinfo> servicelist = activitymanger.getrunningservices(30);// 从窗口管理器中获取正在运行的service tv.settext(getservicesname(servicelist)); setcontentview(tv); } private boolean serviceisstart(list<activitymanager.runningserviceinfo> 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<activitymanager.runningserviceinfo> list) {// 获取所有服务的名称 string res = ""; for (int i = 0; i < list.size(); i++) { res += list.get(i).service.getclassname() + "/n"; } return res; } }