显现:
<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());
}
}
}:
我遇到了同样的问题,但没有找到问题的根源。这似乎是设备或操作系统版本特定的问题。
要知道该消息已被调用,您可以有一个静态布尔值,它在连接和断开连接之间切换,并且仅在收到连接并且布尔值为 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;
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)