2

I have surfed the web and I haven't found a solution to my problem.

In my android app I have to catch and send a notification to the server everytime the user turn off the GPS. At this time I have writed this code

In the Android manifiest:

    <receiver android:name="proguide.prosegur.scr.BL.receivers.GPSStatusBroadcastReceiver">
        <intent-filter>
            <action android:name="android.location.PROVIDERS_CHANGED" />
        </intent-filter>
    </receiver>

In the GPSStatusBroadcastReceiver class:

public class GPSStatusBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context arg0, Intent arg1) {
    if (arg1.getAction().matches("android.location.PROVIDERS_CHANGED")) {
        // here I have to send the notification
    }
}

The problem is that everytime the user put down the GPS, I get this function called twice with identical Context and Intent arguments (I can only send 1 notification at a time).

Important note: it has to work under API level 8.

So, why this happen twice? What can I do (doing it right, not messing up the code) to send only 1 notification at a time? Thanks, sorry for my English.

MMG
  • 3,226
  • 5
  • 16
  • 43
  • How do you call "onReceive"? Do you use a loop? – BrunoMartinsPro Jun 29 '15 at 18:51
  • Just check the `isProviderEnabled` method to see if it return `false`. See my answer below. – ChuongPham Jun 29 '15 at 19:08
  • From what I have understood, putting that line in the manifest makes your app call onRecive method in GPSStatusBroadcastReceiver class every time the app fire an "android.location.PROVIDERS_CHANGED", because you set it that way. Then, you MUST implement the onRecive method from BroadcastReceiver class. – Agustin Zanini Jun 29 '15 at 19:17
  • I would register the `BroadcastReceiver` via a `Service` instead of declaring the `BroadcastReceiver` action in your `AndroidManifest.xml` file. See my updated answer. – ChuongPham Jun 29 '15 at 19:24
  • That's because you define it twice- in your manifest file as well as in your `GPSStatusBroadcastReceiver`. So naturally you will get the GPS function called twice. What behaviour did you expect? –  Jun 29 '15 at 19:39
  • How did you solve it, and why is it called two times? – powder366 Aug 03 '15 at 13:41
  • I solved it using the Location api provided by android :) – Agustin Zanini Aug 06 '15 at 17:41

2 Answers2

2

Try this:

public class GpsReceiver extends BroadcastReceiver {        
    @Override
    public void onReceive(Context context, Intent intent) {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.FROYO) {
            final String action = intent.getAction();
            if (action.equals(LocationManager.PROVIDERS_CHANGED_ACTION)) {
                // GPS is switched off.
                if (!context.getSystemService(Context.LOCATION_SERVICE).isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                    // Do something.
                }
            } 
        }
    }
}

Also, instead of hardcoding "android.location.PROVIDERS_CHANGED", you should use the variable LocationManager.PROVIDERS_CHANGED_ACTION provided by Android.

Instead of setting your GPS receiver in your AndroidManifest.xml file, register your GPS receiver via a Service as follow:

public class GpsService extends Service {
    private BroadcastReceiver mGpsReceiver;
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
       super.onStartCommand(intent, flags, startId);
       registerReceiver();
       return Service.START_NOT_STICKY;
    }
    private void registerReceiver() {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.FROYO) {
            IntentFilter mIntentFilter = new IntentFilter();
            mIntentFilter.addAction(LocationManager.PROVIDERS_CHANGED_ACTION);
            this.mGpsReceiver = new GpsReceiver();
            this.registerReceiver(this.mGpsReceiver, mIntentFilter);
        }       
    }
}
ChuongPham
  • 4,761
  • 8
  • 43
  • 53
  • It seems to have the same behavior as before. And, sincerely, I don't see the difference. Thanks anyway! – Agustin Zanini Jun 29 '15 at 19:14
  • You might have called your GPS receiver multiple times. Instead of setting your GPS receiver in your `AndroidManifest.xml` file, register your GPS receiver via a `Service`. That's how I would do it. – ChuongPham Jun 29 '15 at 19:19
  • Yes, I have also seen this response before. The thing is that it must run under API level 8, that's code, specifically this line `mIntentFilter.addAction(LocationManager.PROVIDERS_CHANGED_ACTION);` needs at least API level 9 – Agustin Zanini Jun 29 '15 at 20:16
  • Then you need to edit your post and state that you want your code to run under API level 8. The `LocationManager.PROVIDERS_CHANGED_ACTION` have only been introduced in API level 9, so I doubt it would work for API level 8. See [here](http://developer.android.com/reference/android/location/LocationManager.html#PROVIDERS_CHANGED_ACTION) for more info. – ChuongPham Jun 29 '15 at 20:21
  • Well.. sorry about that detail ! You were very helpfull anyway. Do you know how to solve this in that way you posted? – Agustin Zanini Jun 30 '15 at 11:57
  • @AgustinZanini: Sorry, I don't know how to apply it to API level 8 unless I dig through Froyo Android source codes and find out. ;) – ChuongPham Jul 03 '15 at 07:36
  • 1
    I finally did it ! For anyone looking for, this is the code: You must create a class (let's say `MyLocationListener`) that implements `LocationListener` There, you have to implement all methods, being one of them `onProviderDisabled(String provider)`.. there you put your code.. Then, you have to register this new class in this way: `MyLocationListener locationListener = new MyLocationListener(startup.this); LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10000,locationListener);` – Agustin Zanini Jul 03 '15 at 14:19
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 broad cast will work once.

sijo jose
  • 130
  • 9