如何检查服务是否在Android 8(API 26)上运行? [英] How to check if a service is running on Android 8 (API 26)?

查看:88
本文介绍了如何检查服务是否在Android 8(API 26)上运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

升级到Android 8后,我的应用程序的某些功能遭到破坏,即使没有明确地针对API 26也是如此.特别是,良好的旧功能可以检查服务是否正在运行(如此处StackOverflow所述:

I had some functionalities of my app broken once upgrading to Android 8, even not targeting the API 26 explicitly. In particular, the good old function to check if a Service is running (as documented on StackOverflow here: How to check if a service is running on Android?) is not working anymore.

只是刷新我们的集体记忆,这是一种经典方法:

Just to refresh our collective memory, it was this classic method:

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

现在的问题是,getRunningServices现在已被弃用,并且不再返回正在运行的服务.有人在Android 8上有解决此问题的经验吗? 有没有官方的解决方案(或黑客)?我还应该指出,我要查找的服务与调用isMyServiceRunning()的代码在同一进程/应用程序中不是(出于向后兼容的原因,仍提供该功能)

The problem now is that getRunningServices is now deprecated and doesn't return the running services anymore. Does anyone have experience with this issue on Android 8? Is there any official solution (or hack) available? I should also point out that the Service I want to look for is not in the same process/app as the code that is calling isMyServiceRunning() (that functionality is still provided for backward compatibility reasons)

推荐答案

getRunningServices()此方法不再对第三方应用程序可用.没有获得运行服务的替代方法.

getRunningServices() this method is no longer available to third party applications. there's no substitute method for getting the running service.

https://developer.android.com/reference/android/app/ActivityManager.html#getRunningServices(int)

如何检查服务是否在Android上运行?) 我只是手动检查它,在服务运行时将布尔值设置为true,而在服务停止或销毁时将布尔值设置为false.我正在使用SharedPreferences保存布尔值.

How to check if a service is running on Android?) I just check it manually , I put Boolean true when service is running and false when the service stopped or destroyed. I'm using SharedPreferences to save the Boolean value.

Service.class

Service.class

override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
    Log.d("service", "onStartCommand")
    setRunning(true)
}

private fun setRunning(running: Boolean) {
    val sessionManager = SessionManager(this)
    sessionManager.isRunning = running
}


override fun onDestroy() {
   setRunning(false)
   super.onDestroy()
}

SessionManager.class

SessionManager.class

class SessionManager(var context: Context) {
    private val loginpreferences: SharedPreferences
    private val logineditor: SharedPreferences.Editor

    init {
      loginpreferences = context.getSharedPreferences(Pref_name, private_modde)
      logineditor = loginpreferences.edit()
    }

    var isRunning: Boolean
      get() = loginpreferences.getBoolean(SERVICES, false)
      set(value) {
         logineditor.putBoolean(SERVICES, value)
         logineditor.commit()
      }

    companion object {
      private val SERVICES = "service"
    }

}

这篇关于如何检查服务是否在Android 8(API 26)上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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