【问题标题】:Broadcast receiver called 2 times when turning off GPS?关闭 GPS 时广播接收器调用 2 次?
【发布时间】:2015-10-25 16:27:50
【问题描述】:

清单:

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

}:

【问题讨论】:

  • 编辑 Log.d(TAG, "onReceive..."); 到这个 -> Log.d(TAG, intent.getAction(); 看看为什么会被调用两次 :)
  • 两次都一样:android.location.PROVIDERS_CHANGED

标签: android android-broadcast


【解决方案1】:

我遇到了同样的问题,但我没有找到问题的根源。似乎是设备或操作系统版本特定的问题。

要知道消息已被调用,您可以有一个静态布尔值,在连接和断开之间切换,并且仅在您收到连接并且布尔值为真时才调用您的子例程。比如:

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

【讨论】:

    【解决方案2】:

    这是我的解决方案代码

    public class MyBroadcastReceivers extends BroadcastReceiver {
    
        static boolean isGpsEnabled,isPermissionAskedOnce=true;
    
        @Override
        public void onReceive(Context context, Intent intent) {
    
            if (intent.getAction().matches(LocationManager.PROVIDERS_CHANGED_ACTION)) {
                LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
                isGpsEnabled = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
                
    
                if(isGpsEnabled){
                       //do you stuff
                        isPermissionAskedOnce=true;
                }else{
                     
                    if(isPermissionAskedOnce){
                      //do your stuff 
                        isPermissionAskedOnce=false;
                    }
                }
            }
        }
    
    
    }
    

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    【解决方案3】:

    为什么不检查您的接收器中是否启用了 GPS 提供程序?

    lm = (LocationManager) context.getSystemService(LOCATION_SERVICE);
    isGPSEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    

    【讨论】:

    • 不应在 onReceive 中进行耗时的调用。我正在通过 EventBus 发送和事件,现在已发送两次。在事件接收器中,我有您提供的代码。但它仍然没有回答我的问题......
    猜你喜欢
    • 1970-01-01
    • 2014-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多