Android - getRunningservices(ActivityManager)已弃用 [英] Android - getRunningservices(ActivityManager) deprecated

查看:2693
本文介绍了Android - getRunningservices(ActivityManager)已弃用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 MyService.java 是否有效?

private boolean isServiceAlive(Class<?> serviceClass) {
    ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (serviceClass.getName().equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}

它工作正常,但 manager.getRunningServices()已从API 26中弃用。那么我们还有另一个很好的解决方案(方法)来获取当前正在运行的服务列表吗?

it works fine but manager.getRunningServices() is deprecated from API 26. So have we another good solution(method) to get a list of the services that are currently running?

推荐答案

尽管它没有回答你的问题,但我认为你仍然可以将这种方法用于你自己的服务:

Despite it doesn't answer your question, I think you still can use this method for your own services:


为了向后兼容,它仍将返回调用者自己的服务。

For backwards compatibility, it will still return the caller's own services.

如果您只想删除弃用警告,使用 @SuppressWarnings(弃用)

If you'd just like to remove the deprecation warning, use @SuppressWarnings("deprecation")

这篇关于Android - getRunningservices(ActivityManager)已弃用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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