检查前台服务是否在 Android OREO 中运行 [英] Check if Foreground service is running in Android OREO

查看:64
本文介绍了检查前台服务是否在 Android OREO 中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查我的服务是否正在运行,我正在使用下面的代码,它在牛轧糖之前工作正常,但在 OREO 中不起作用

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..

推荐答案

ActivityManager.getRunningServices() API 已经自 API 26 起已弃用,并且自 Oreo 起不再可用.它旨在仅用于调试,而不是用于生产应用程序.您不应将这种类型的 API 用于您的应用.

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.

这篇关于检查前台服务是否在 Android OREO 中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆