Search code examples
javaandroidbroadcastreceiverlocationmanagerandroid-gps

How to stop GPS BroadcastReceiver


I use broadcastreceiver on my gps that check if an user disabled GPS while app working. If the user disabled, then shows a dialog with information that a GPS is requaired.

This is my sample in manifest:

    <receiver android:name=".BroadCastGPS">
        <intent-filter>
            <action android:name="android.location.PROVIDERS_CHANGED" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

This is my BroadCastGPS class:

public class BroadCastGPS extends BroadcastReceiver {

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

        if(intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {

            LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

            if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {

                if(!gpsStatus.GPS) {

                    Intent pushintent = new Intent(context, gpsStatus.class);
                    pushintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(pushintent);

                }

            } else {

                if(gpsStatus.GPS) {

                    Intent pushintent = new Intent(context, Online.class);
                    pushintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(pushintent);

                }

            }
        }

    }
}

My problem is that, when an user closed this app, after few seconds shows dialog with information, that GPS is requaired. How can I stop broadcast before close app? I try use System.exit(0), kill process but it not works.

This sample is called while close app:

PackageManager pm = mContext.getPackageManager();
ComponentName compName =
        new ComponentName(mContext,
                BroadCastGPS.class);
pm.setComponentEnabledSetting(
        compName,
        PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
        PackageManager.DONT_KILL_APP);

Solution

  • Never try force closing you Application. Android handles that. If you want to turn off your compenent use setComponentEnabledSetting