Android: Check if service is running via. bindService

前端 未结 7 1462
心在旅途
心在旅途 2021-02-18 15:28

What would be the best way to check if an Android Service is running? I am aware of the ActivityManager API, but it seems like the use of the API is not advised for

7条回答
  •  渐次进展
    2021-02-18 16:24

    Another method:

    public static boolean isServiceRunning(Context ctx, String serviceClassName) {
        ActivityManager manager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
        for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (TrackingService.class.getName().equals(serviceClassName)) {
                return true;
            }
        }
        return false;
    }
    

提交回复
热议问题