Aishwat Singh
Aishwat Singh

Reputation: 4459

Android Marshmallow, locationManager.isProviderEnbled always return false

        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        //getting GPS status
        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        //getting network status
        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

Both are false, Although i did check for Marshmallow , there's something like runtime permissions, so i followed these links link1 and link2

And did some debugging and putting checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION); before isNetworkEnabled returns 0 , which is PERMISSION_GRANTED

Code

SplashScreenActivity

public class SplashScreenActivity extends Activity {

    private static final String TAG = "SplashScreenActivity";
    boolean isGPSEnabled = false;

    // flag for network status
    boolean isNetworkEnabled = false;

    final private int REQUEST_CODE_ASK_PERMISSIONS = 123;

    @TargetApi(Build.VERSION_CODES.M)
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.activity_splashscreen);

        int hasLocationPermission = checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION);
        if (hasLocationPermission != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    REQUEST_CODE_ASK_PERMISSIONS);
            return;
        }
        //comes till here . ie hasLocationPermission is true 
        LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);

        checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION); //breakpoint at this line gives 0 ie PERMISSION_GRANTED

        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); //false at debug breakpoint
        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);//false at debug breakpoint

    }


}

Manifest Here

Upvotes: 1

Views: 1305

Answers (2)

D_Alpha
D_Alpha

Reputation: 4069

Use this piece of code , It will work on all the android versions-

Create a boolean type method to check that user has the permissions or not-

 private boolean checkPermission() {

    boolean flag = true;

    String[] permissions = {"android.permission.ACCESS_FINE_LOCATION", "android.permission.ACCESS_COARSE_LOCATION"};
    boolean hasPermission = (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED);
    if (!hasPermission) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(permissions, com.jeenees.androidapp.utils.Keys.FILE_READ_PERMISSION);
            flag = false;
        }
    } else {
        flag = true;
    }

    return flag;
}

Use the above method where you want like-

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

 if (checkPermission()){
            isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); //false at debug breakpoint
            isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);//false at debug breakpoint
  }

Hope this will help you.

Upvotes: 2

Aishwat Singh
Aishwat Singh

Reputation: 4459

The issue is with Device , i tried with Nexus 5x and same piece of code returns true. Issue is with latest marshmallow update of samsung

Upvotes: 1

Related Questions