0

The main activity is destroyed the first time the app is installed and after that I've got a background service that runs whenever there is available internet.

The background service detects location (amongst other things) and since the app is supposed to remain on the phone for months (it only works when there is internet and not all the time or else that'd drain the battery even further) well since it runs for a long time, the user can disable gps and forget to turn it back on before he is connected to internet the next time which can cause the app to crash when the google API client requests location so for now, the background app automatically opens the settings interface when it detects that gps is off in order to remind the user to turn it on but this solution isn't that good since the interface appears out of nowhere and the user can get confused.

So I want to make a prompt/alert dialogue that tells the user "the app [appName] requires gsp please turn it on" with "yes" and "no" choices and when the user clicks "yes" then the settings interface is shown. Is this doable ? Again, I'm not looking for a solution with an activity as I already know how to do this (no transparent activity either just using the service)

Here's how I'm doing things so far:

//This part of the code is right before getting the current location
    if (!isLocationEnabled(this)) {

              Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
              myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              startActivity(myIntent);

        }

    public static boolean isLocationEnabled(Context context) {
        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 false;
            }

            return locationMode != Settings.Secure.LOCATION_MODE_OFF;

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


    }
student93
  • 307
  • 2
  • 12
  • public void requestCamera(Callback callback) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && (checkSelfPermission(Manifest.permission.CAMERA)) != PackageManager.PERMISSION_GRANTED) { callbacks.put(33,callback); ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 33); } else { callback.call(true); } } – Henning Luther Apr 04 '17 at 18:22
  • @HenningLuther I'm confused. What does "camera" have to do with my issue ? I'm looking for a solution to prompt user to activate gps – student93 Apr 04 '17 at 18:56
  • Was the example for camera, and its the same for every permission, you just need to find the global-constant for your GPS permission and change that. Im confused that you can not see that. Was just to lazy to find it for you and perhaps you can find it by yourself, if not give me 20sec. – Henning Luther Apr 04 '17 at 18:59
  • okay then. I'll try to follow your example and do the same with GPS permissions – student93 Apr 04 '17 at 19:02
  • good! seems you are a student, so finding out by yourself is a good thing to do and to learn how to. You also dont need the callback stuff, its just for synchronizing in a concurrent scenario, so perhaps for you its not nesseccary. try to remove and change to for all you need. – Henning Luther Apr 04 '17 at 19:04
  • and when its working for you i post a complete answer, or even if you want me to do now i can do. but to be honest it normally will be a duplicate in stackoverflow: http://stackoverflow.com/questions/33865445/gps-location-provider-requires-access-fine-location-permission-for-android-6-0 – Henning Luther Apr 04 '17 at 19:06
  • Asking for gps permissions since android 6: http://stackoverflow.com/questions/33865445/gps-location-provider-requires-access-fine-location-permission-for-android-6-0 – Henning Luther Apr 04 '17 at 19:07
  • @HenningLuther In fact I did the fine location permission check (in a previous part of the code) but isn't this permission required only once ? as in, once it's granted the user isn't prompted again because the app already has location permission ? so if the user turns off gps, the app still has the location permission but the gps is off. This is my problem, how to prompt the user to turn on gps when the app already has location permission. – student93 Apr 04 '17 at 19:15
  • ahh, ok, i didnt get this problem, so the answer below perhaps helps you – Henning Luther Apr 04 '17 at 19:20

1 Answers1

0

You can implement in your service

Location. Locationllistener 

And use override method

LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); // Define a listener that responds to location updates LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { // Called when a new location is found by the network location provider. makeUseOfNewLocation(location); } public void onStatusChanged(String provider, int status, Bundle extras) {} public void onProviderEnabled(String provider) {} public void onProviderDisabled(String provider) {// here if provider is gps show notification } }; // Register the listener with the Location Manager to 

Inside this method you can make notification with alert sound

Elsunhoty
  • 1,609
  • 14
  • 27