Hello guys I am using Broadcast Receiver in my project that gives me multiple outputs at one time while enabling and disabling GPS. How to solve that??
Here is a log that will give multiple outputs like this
MyBroadcastReceivers﹕ IsGPS = true
MyBroadcastReceivers﹕ IsGPS = true
MyBroadcastReceivers﹕ IsGPS = true
MyBroadcastReceivers﹕ IsGPS = true
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;
}
}
}
}
}