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

}:
powder366
  • 4,351
  • 7
  • 47
  • 79

3 Answers3

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;
          }
      }
  }
Harshal
  • 131
  • 1
  • 5
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;
                }
            }
        }
    }


}
Manish
  • 170
  • 9
  • 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](/help/how-to-answer). – Community Dec 09 '21 at 20:51
-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);
formica
  • 934
  • 1
  • 8
  • 16
  • 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... – powder366 Aug 03 '15 at 15:39