1
        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

Aishwat Singh
  • 4,331
  • 2
  • 26
  • 48
  • 1
    Where are you executing this? In an emulator or on a real device? If real, then please state: Model, Build (OS version and build number) and any logcat that may be relevant.... Most likely it seems you have 2 different Activities or whatevers that 1 has permissions, and 1 does not.... also `hasWriteContactsPermission` checking for GPS is bad practice... use names that another person may read and understand EXACTLY what that variable is.... the same applies in `Build.VERSION.SDK_INT >= 23` [chek this](http://stackoverflow.com/questions/47882/what-is-a-magic-number-and-why-is-it-bad) – Bonatti Mar 28 '16 at 11:22
  • Please check this once. http://stackoverflow.com/questions/16748300/locationmanager-isproviderenabledlocationmanager-network-provider-is-not-relia?lq=1 – Raghu Nagaraju Mar 28 '16 at 11:24
  • @Bonatti updated it to hasLocationPermission, executing this in device : Samsung Edge 6 , android M, android ver 6.0.1 build# MMB29K.G925IDVU3EPC5, logs here http://pastebin.com/9WstqwL1 , however by debugging i am able to pinpoint that issue is at locationManager.isProviderEnabled only – Aishwat Singh Mar 28 '16 at 11:31
  • @Bonatti what u mean by `1 has permissions, and 1 does not` i checked in GPS Tracker class by debugging and it has permission – Aishwat Singh Mar 28 '16 at 11:38
  • Its better, but still, if we are to help, information is a must.... on `getLocation()`, put a Log.v(TAG, "hasLocationPermission "+hasLocationPermission);` on it, then paste the LogCat on your question, so that others might help. And what is the `android` Object that appears in that function? Finally, add some details on your application... It appears that you want the result on a linear code execution, while GPS might take a while to gather data. – Bonatti Mar 28 '16 at 11:40

2 Answers2

2

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.

D_Alpha
  • 4,039
  • 22
  • 36
1

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

Aishwat Singh
  • 4,331
  • 2
  • 26
  • 48