Search code examples
androidandroid-gps

How to get current location in Nougat (7.1.1)?


I am developing one android application in that i am getting current location of user, following code work fine in below 7.1.1 version of android but not working on 7.1.1, i am getting null for location

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

        Criteria criteria = new Criteria();
        if (ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            List<String> providers = locationManager.getAllProviders();
            Location location = null;

            if (android.os.Build.VERSION.SDK_INT >= 25)
            {
                location = locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);
            }

            else {
                for (String str : providers) {

                    location = locationManager.getLastKnownLocation(str);
                    if (location != null)
                        break;
                }
            }

how do i get current location in android 7.1.1 version?


Solution

  • Why not use the FusedLocationProvider like @thepoosh said.

    • This give you less batterydrain, and easier use!

    Here is a reference with all the info about the FusedLocationProvider.

    Permission in the manifest:

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    

    Runtime permissions: (this is the code I use -> not the cleanest but it should help you start...)

    public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
    
        private static final String TAG = "MainActivity";
        private static final int REQUEST_PERMISSIONS_REQUEST_CODE = 1;
    
        private FusedLocationProviderClient mFusedLocationClient;
        private LocationCallback mLocationCallback;
        private LocationRequest mLocationRequest;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mLocationCallback = new LocationCallback() {
                @Override
                public void onLocationResult(LocationResult locationResult) {
                    super.onLocationResult(locationResult);
                    //Here is your location result
                }
            };
            mLocationRequest = LocationHelper.createLocationRequest();
            mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
    
            if (LocationHelper.checkLocationPermissions(this)) {
                startGps();
            } else {
                requestPermissions();
            }
        }
    
        private void requestPermissions() {
            boolean shouldProvideRationale =
                    ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION);
    
            // Provide an additional rationale to the user. This would happen if the user denied the
            // request previously, but didn't check the "Don't ask again" checkbox.
            if (shouldProvideRationale) {
                Log.i(TAG, "Displaying permission rationale to provide additional context.");
                View container = findViewById(android.R.id.content);
                DialogHelper.showSnackbar(this, R.string.permission_rationale, android.R.string.ok,
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                // Request permission
                                ActivityCompat.requestPermissions(MainActivity.this,
                                        new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                                        REQUEST_PERMISSIONS_REQUEST_CODE);
                            }
                        });
            } else {
                Log.i(TAG, "Requesting permission");
                // Request permission. It's possible this can be auto answered if device policy
                // sets the permission in a given state or the user denied the permission
                // previously and checked "Never ask again".
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
                        REQUEST_PERMISSIONS_REQUEST_CODE);
            }
        }
    
        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            Log.i(TAG, "onRequestPermissionResult");
            if (requestCode == REQUEST_PERMISSIONS_REQUEST_CODE) {
                if (grantResults.length <= 0) {
                    // If user interaction was interrupted, the permission request is cancelled and you
                    // receive empty arrays.
                    Log.i(TAG, "User interaction was cancelled.");
                } else if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Log.i(TAG, "Permission granted.");
                    startGps();
                } else {
                    // Permission denied.
    
                    // Notify the user via a SnackBar that they have rejected a core permission for the
                    // app, which makes the Activity useless. In a real app, core permissions would
                    // typically be best requested during a welcome-screen flow.
    
                    // Additionally, it is important to remember that a permission might have been
                    // rejected without asking the user for permission (device policy or "Never ask
                    // again" prompts). Therefore, a user interface affordance is typically implemented
                    // when permissions are denied. Otherwise, your app could appear unresponsive to
                    // touches or interactions which have required permissions.
    
                    //TODO: SHOW SNACKBAR
    
                }
            }
        }
    
        private void startGps()
        {
            Log.i(TAG, "Setting up location services...");
            mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, null).addOnSuccessListener(this).addOnFailureListener(this);
        }
    }
    

    The LocationHelper:

    public class LocationHelper {
    
        private static final String TAG = "LocationHelper";
        private static final int UPDATE_INTERVAL = 4000;
        private static final int FASTEST_UPDATE_INTERVAL = 2000;
    
        public static LocationRequest createLocationRequest()
        {
            LocationRequest locationRequest = new LocationRequest();
            locationRequest.setInterval(UPDATE_INTERVAL);
            locationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL);
            locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
            return locationRequest;
        }
    
        public static boolean checkLocationPermissions(Context context) {
            return ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                    || ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
        }
    }