0

I'm testing out a Broadcast Receiver and printing the state of GPS in my logs to get a feel for if it works as intended, and I found some odd behavior (maybe it isn't odd, but I didn't expect it.)

Whenever one toggled GPS, if it was the first time in a while, it'll take a while for the BroadcastReceiver to receive the event, and when it does, it fires off many times (around 10). After that, when one toggles it on and off, it gets fired off twice each time.

Why is this?

This is what I have in my manifest:

<receiver android:name=".receivers.GpsLocationReceiver" >
    <intent-filter>
        <action android:name="android.location.PROVIDERS_CHANGED" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>
David
  • 7,028
  • 10
  • 48
  • 95

1 Answers1

0

You can avoid this problem using sharedpreference and with an thread but it is not a proper way to overcome this problem

my method as follows

 @Override
    public void onReceive(Context context, Intent intent) {

boolean flage=MainActivity.getpreference();

if(!flage){
    MainActivity.putPreferens(true);
    Log.e("gpssss","gpssss");

    Thread thread = new Thread() {
        @Override
        public void run() {
            try {




                    sleep(2000);

                    MainActivity.putPreferens(false);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };

    thread.start();


     }}

}

to the main class am create a sharedpreference and store boolean value false the the broad cast

sijo jose
  • 130
  • 9