0

I am creating an app and i am getting error in my location access of mobile in app. please help me. using following code......

  public static boolean isLocationEnabled(Context context) {
    int locationMode = 0;
    String locationProviders;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
        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);
    }


}

i am getting error in this

"locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);"

please tell me how to resolve it. Thanks click here what i want

Rishabh
  • 51
  • 8
  • did you get permission from user? new os 6.0 you may need to acquire permission during runtime and please provide your logcat. – kggoh Aug 03 '16 at 06:54
  • this my logcat "Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ContentResolver android.content.Context.getContentResolver()' on a null object reference at com.test.MapsActivity.isLocationEnabled(MapsActivity.java:179)" – Rishabh Aug 03 '16 at 06:55
  • doesn't seem like a permission problem yet. it seems like it complaint about your context. Did you pass in the right context? where you call this function? in activity or fragment? – kggoh Aug 03 '16 at 06:59
  • in OnCreate "isLocationEnabled(context);" Like this – Rishabh Aug 03 '16 at 07:00
  • this is how i called in my activity. Utils.isLocationEnabled(this); result successful, shown in screenshot below. – kggoh Aug 03 '16 at 07:14
  • please click on " click here what i want" in question below – Rishabh Aug 03 '16 at 07:34

1 Answers1

0

enter image description here

I tried your code, I am able to get location. Then you better check if your context is right. Permission required?

kggoh
  • 742
  • 1
  • 10
  • 24
  • i want when i click on "allow button" location access automatically ON – Rishabh Aug 03 '16 at 07:15
  • no, when i open my app at that my app not getting location ON.so that time i click on "allow button" location access is automatically ON – Rishabh Aug 03 '16 at 07:19
  • please click on " click here what i want" in question below – Rishabh Aug 03 '16 at 07:34
  • Is this what you want? [link] http://stackoverflow.com/questions/8863509/how-to-programmatically-turn-off-wifi-on-android-device – kggoh Aug 03 '16 at 07:48
  • i donot want wifi I want only location permission access – Rishabh Aug 03 '16 at 07:51
  • it is all in stackoverflow, all you need is just search [link] http://stackoverflow.com/questions/25175522/how-to-enable-location-access-programatically-in-android – kggoh Aug 03 '16 at 08:06