How to catch GPS off broadcast once?

穿精又带淫゛_ 提交于 2021-02-10 07:16:06

问题


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.


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/31123253/how-to-catch-gps-off-broadcast-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!