Broadcast receiver called 2 times when turning off GPS?
Asked Answered
D

3

6

Manifest:

<receiver android:name=".GpsLocationReceiver">
    <intent-filter>
        <action android:name="android.location.PROVIDERS_CHANGED" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>

BroadcastReceiver:

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

}:
Dorty answered 3/8, 2015 at 14:5 Comment(2)
Edit the Log.d(TAG, "onReceive..."); to this -> Log.d(TAG, intent.getAction(); to see why is called twice :)Mongolian
The same both times: android.location.PROVIDERS_CHANGEDDorty
T
4

I faced same problem but I did't find root of problem.It seems device or OS version specific problem.

To know that the message has been called, you could have a static boolean that gets toggled between connect and disconnect and only call your sub-routines when you receive a connection and the boolean is true. Something like:

  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;
          }
      }
  }
Thebault answered 8/9, 2015 at 9:47 Comment(0)
P
2

Here is my Solution code

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


}
Penitentiary answered 9/12, 2021 at 20:17 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Stagnate
B
-1

Why not check whether the GPS provider is enabled in your receiver?

lm = (LocationManager) context.getSystemService(LOCATION_SERVICE);
isGPSEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
Brainwashing answered 3/8, 2015 at 15:34 Comment(1)
One should not have time consuming calls in onReceive. I'm sending and event via EventBus and now it's sent two times. In the event receiver I have the code you provide. But it still does not answer my question...Dorty

© 2022 - 2024 — McMap. All rights reserved.