Check if Foreground service is running in Android OREO

牧云@^-^@ 提交于 2020-05-12 04:55:47

问题


I want to check whether my service is running in or not, I was using below code which was working fine till nougat but not working in OREO

 private boolean isMyServiceRunning(Class<?> serviceClass, Context context) {
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            Log.d(TAG, "isMyServiceRunning: " + service.service.getClassName());
            if (serviceClass.getName().equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }

But above code is not working for foreground services.

I have referred this but it is also not working..


回答1:


The ActivityManager.getRunningServices() API has been deprecated since API 26 and is no longer available as of Oreo. It was intended to only be used for debugging, not production apps. You should not rely on this type of API for your app.



来源:https://stackoverflow.com/questions/53761074/check-if-foreground-service-is-running-in-android-oreo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!