代码之家  ›  专栏  ›  技术社区  ›  Amin Pinjari

检查前台服务是否在Android OREO中运行

  •  0
  • Amin Pinjari  · 技术社区  · 7 年前

    我想看看我的服务是否在运行, 我在用下面的代码,在努加特之前运行良好,但在奥利奥不工作

     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;
        }
    

    我已经提到 this 但也不管用。。

    1 回复  |  直到 7 年前
        1
  •  3
  •   Mikhail Olshanski    7 年前

    这个 ActivityManager.getRunningServices() API已经 deprecated 自API 26起,从Oreo起不再提供。它只用于调试,而不是生产应用。你的应用不应该依赖这种类型的API。

    推荐文章