java Android - 不推荐使用 getRunningservices(ActivityManager)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45519439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - getRunningservices(ActivityManager) deprecated
提问by Makhanov Madiyar
I want to know MyService.java
is working or not?
我想知道MyService.java
是否有效?
I create this method:
我创建了这个方法:
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;
}
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?
它工作正常,但manager.getRunningServices()
已从 API 26 中弃用。那么我们是否有另一个好的解决方案(方法)来获取当前正在运行的服务列表?
采纳答案by geiger
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.
为了向后兼容,它仍然会返回调用者自己的服务。
If you'd just like to remove the deprecation warning, use @SuppressWarnings("deprecation")
如果您只想删除弃用警告,请使用 @SuppressWarnings("deprecation")
回答by Radesh
Welcome to Android O
欢迎使用 Android O
Another Annoying bug of android Oreo, there is just Deprecatedannotation and there is no other way to do this. but maybe in the past android developers decide to create alternative function.
android Oreo 的另一个烦人的错误,只有Deprecated注释,没有其他方法可以做到这一点。但也许过去 android 开发人员决定创建替代功能。
For now as Documentsays continue to use getRunningServices
and use @SuppressWarnings("deprecation")
above of your function to get rid of warning.
现在正如Document所说,继续使用getRunningServices
和使用@SuppressWarnings("deprecation")
上面的函数来消除警告。
As of Build.VERSION_CODES.O, this method is no longer available to third party applications. For backwards compatibility, it will still return the caller's own services.
自 Build.VERSION_CODES.O 起,此方法不再适用于第三方应用程序。为了向后兼容,它仍然会返回调用者自己的服务。
回答by Radesh
Here's what you do. If the services you are using are within your own application, just make a singleton that has a boolean variable for started or stopped. When the service starts, make sure to update that variable.
这就是你要做的。如果您使用的服务在您自己的应用程序中,只需创建一个具有用于启动或停止的布尔变量的单例。当服务启动时,确保更新该变量。
public class SingletonServiceManager {
public static boolean isMyServiceRunning = false;
}
The above is literally all i use for a small singleton class, later, in some other class, where you want to know if the service is running...
以上就是我在一个小的单例类中使用的所有内容,稍后,在其他一些类中,您想知道服务是否正在运行......
if(SingletonServiceManager.isMyServiceRunning == true){
// do something
}
You just have to manage where and when this value is updated. For instance, set it to true when you start the service (inside the service). Then, update the boolean to false when you stop the service (from inside the service)
您只需要管理更新此值的位置和时间。例如,在启动服务时(在服务内部)将其设置为 true。然后,在停止服务时将布尔值更新为 false(从服务内部)