gpt4 book ai didi

Android:检查我的服务是否在后台运行

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:28:27 29 4
gpt4 key购买 nike

我正在运行一个服务,以便在我的应用程序中每 1 小时接收一次通知

该服务肯定每 1 小时运行一次;我使用 Logcat 检查了它。

但是因为我是在我的 MainActivity 的 onResume() 中启动这个服务的,所以我想检查这个服务是否已经在运行,只有当它没有运行时才启动它。

我使用了这段代码:

private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if ("com.android.MyApp.MyServices.NotificationsService".equals(service.service.getClassName())) {
return true;
}
}
return false;
}

但我总是得到

所以我检查了 service.service.getClassName() 给出的输出。我的服务不在列表中。

是不是因为我的申请未知?还是别的?

最佳答案

我知道有点晚了,但这个答案可能会帮助其他正在寻找相同答案的人。

您是否使用 context.startService() 启动了您的服务?
确保 list 文件包含以下内容:

<service android:name="com.android.MyApp.MyServices.NotificationsService" />

关于Android:检查我的服务是否在后台运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12891903/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com