gpt4 book ai didi

android如何检查服务是否正在运行

转载 作者:太空宇宙 更新时间:2023-11-03 12:35:53 25 4
gpt4 key购买 nike

我有一个获取 GPS 坐标的应用程序。它工作正常,但如果服务正在运行,我想从调用 Activity 中进行测试。我有以下我认为会检查的代码,但是当通知栏中的 GPS 图标仍在闪烁时它返回 false。有谁知道为什么?提前致谢

private boolean isGpsServiceRunning() {
ActivityManager manager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if ("LocationService".equals(service.service.getClassName())) {
return true;
}
}
return false;
}

.

LocalBroadcastManager.getInstance(getApplicationContext())
.registerReceiver(
locationChangereceiver,
new IntentFilter(
LocationChangeIntent.ACTION_LOCATION_CHANGE));
startService(new Intent(this, LocationService.class));

最佳答案

您应该与 LocationService.class.getName() 进行比较,而不是使用硬编码值。此外,您使用的值对应于 Class#getSimpleName()这可能不是您想要的。

关于android如何检查服务是否正在运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19429140/

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