Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 广播接收器在关闭GPS时调用了2次?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
表现:

@H_944_7@<receiver android:name=".GpsLOCATIOnReceiver"> <intent-filter> <action android:name="android.LOCATIOn.PROVIDERS_CHANGED" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver>

广播接收器:

@H_944_7@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()); } } }:

解决方法

@H_450_17@ 我遇到了同样的问题,但我找不到问题的根源.它似乎是设备或操作系统版本特定的问题.

要知道消息已被调用,你可以有一个静态布尔值,它在connect和disconnect之间切换,只在你收到一个连接并且Boolean为true时调用你的子例程.就像是:

@H_944_7@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; } } }

大佬总结

以上是大佬教程为你收集整理的android – 广播接收器在关闭GPS时调用了2次?全部内容,希望文章能够帮你解决android – 广播接收器在关闭GPS时调用了2次?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。