我有一个获取GPS坐标的应用程序。它工作正常,但我想从调用活动内测试如果服务正在运行。我有以下代码,我认为会检查,但当通知栏中的GPS图标仍然闪烁时,它会返回false。有没有人有任何想法?在此先感谢android如何检查服务是否正在运行
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.getSimpleName()而不是硬编码它。 –
只是修复它,我也需要把包名称。和你说的一样。创造一个答案,我会接受。谢谢 – turtleboy
好吧,完成:) –