0

I followed this to Enable GPS Programatically

So here its working with Ok and cancel Buttons

so if User Press Cancel Button App will exit and if he press ok app will work fine with GPS.

Here In my application I have 12 activities all activities need GPS(Location)

With that example its works fine but If user disable GPS manually What should I do I need to add the Same code for all 12 activities...? can any one suggest me how to monitor GPS status on or off on each activity and if User disable GPS after opening a activity App should close...

I already Tried this but its working only for one time

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    if (requestCode == 1000) {
        if(resultCode == Activity.RESULT_OK)
        {
            String result=data.getStringExtra("result"); 
        }
        if (resultCode == Activity.RESULT_CANCELED) {
            finish();
            System.exit(0);
        } 
    } 
} 
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Whats Going On
  • 1,379
  • 6
  • 20
  • 49

6 Answers6

2

At the start of your application make the user enable his GPS using your code which you have written.

Then, create a BroadcastReceiver like this

public class GPSChangedReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Toast.makeText(context, "GPS status changed", Toast.LENGTH_SHORT).show();
     // Your code to enable GPS again
     // Give Alert to eneable GPS again
     // Any other task that you want to perform
    }
}

And register it in your manifest.xml

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

This BroadcastReceiver will get called every time user changes the state of his GPS, so now whenever the user tries to on/off his GPS the broadcast will get called and you can give him alert dialogue or force him to enable his GPS.

Akshay Katariya
  • 1,464
  • 9
  • 20
0

Request the permission only in starting activities on the app start (in onResume for example), the ones which have intent-filter defined in AndroidManifest.xml.

Anton Malyshev
  • 8,686
  • 2
  • 27
  • 45
0

Create one BaseActivity and extend all your 12 acitivties by this BaseActivity

Do all location related work in base

Satender Kumar
  • 627
  • 4
  • 7
0

you can create BaseActivity.class and extend that in all your activities. Then add dialog for location check in onResume of BaseActivity and navigate user accordingly.

Aj 27
  • 2,316
  • 21
  • 29
0

check for location is enable or not with this method. you can directly call this method in onStart()

int locationMode = 0;
        String locationProviders;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            try {
                locationMode = Settings.Secure.getInt(
                        context.getContentResolver(),
                        Settings.Secure.LOCATION_MODE);

            } catch (Settings.SettingNotFoundException e) {
                e.printStackTrace();
            }

            return locationMode != Settings.Secure.LOCATION_MODE_OFF;

        } else {
            locationProviders = Settings.Secure.getString(
                    context.getContentResolver(),
                    Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
            return !TextUtils.isEmpty(locationProviders);
        }

return that value and if it is not enabled then use following method

 public void showGPSAlert() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.location_not_enabled); // GPS not found
        builder.setMessage(R.string.location_access_enable); // Want to enable?
        builder.setPositiveButton(R.string.ok,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Intent viewIntent = new Intent(
                                Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        startActivityForResult(viewIntent, LOCATION_ENABLED);
                    }
                });
        builder.create().show();
        return;

    }
MaYur MahAjan
  • 143
  • 10
0

At a common place (say BaseActivity of all your 12 activities) You can register to listen to GPS status

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.addGpsStatusListener(new android.location.GpsStatus.Listener()
{
    public void onGpsStatusChanged(int event)
    {
        switch(event)
        {
        case GPS_EVENT_STARTED:
            // GPS is switched ON
            break;
        case GPS_EVENT_STOPPED:
            // GPS is switched off
            break;
        }
    }
});
Zeba
  • 3,041
  • 1
  • 28
  • 39