Get the running Service instance in Android
- 2020-05-09 19:19:14
- OfStack
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); // get Activity manager
List
<
ActivityManager.RunningServiceInfo
>
serviceList = activityManger. getRunningServices (30); // get the running Service from the window manager
tv.setText(getServicesName(serviceList));
setContentView(tv);
}
private boolean ServiceIsStart(List
<
ActivityManager.RunningServiceInfo
>
list, String className) {// determine whether a service is started
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) {// gets the names of all the services
String res = "";
for (int i = 0; i
<
list.size(); i++) {
res += list.get(i).service.getClassName() + "/n";
}
return res;
}
}