清单:
<receiver android:name=".GpsLocationReceiver">
<intent-filter>
<action android:name="android.location.PROVIDERS_CHANGED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
广播接收器:
public class GpsLocationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive...");
if(intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
Log.d(TAG, "GPS provider changed...");
EventBus.getDefault().postLocal(intent.getAction());
}
}
}:
答案 0 :(得分:1)
我遇到了同样的问题,但我找不到问题的根源。它似乎是设备或操作系统版本特定的问题。
要知道消息已被调用,你可以有一个静态布尔值,它在connect和disconnect之间切换,只在你收到一个连接并且boolean为true时调用你的子例程。类似的东西:
private static boolean firstConnect = true;
@Override
public void onReceive( Context context, Intent intent )
{
//Receive called twice because of device or OS version specific issue.
final LocationManager manager = (LocationManager) context.getSystemService( Context.LOCATION_SERVICE );
if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
//enable
if(firstConnect){
sendStatus("on",context);
firstConnect=false;
}
}else{
//disable
if(!firstConnect){
sendStatus("off",context);
firstConnect=true;
}
}
}
答案 1 :(得分:-1)
为什么不检查接收器中是否启用了GPS提供商?
lm = (LocationManager) context.getSystemService(LOCATION_SERVICE);
isGPSEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);