how to ask user for his location in android

Solutions on MaxInterview for how to ask user for his location in android by the best coders in the world

showing results for - "how to ask user for his location in android"
Luciano
10 Aug 2018
1public class MainActivity extends AppCompatActivity {
2  
3   //'LocationManager' manages location tracking
4   /*'LocationListener' gives us updates about whenever the device moves, it gives us updates on all those */
5
6    LocationManager locationManager;
7    LocationListener locationListener;
8
9    @Override
10    protected void onCreate(Bundle savedInstanceState) {
11        super.onCreate(savedInstanceState);
12        setContentView(R.layout.activity_main);
13
14        //STEP 1: Declare and Initialize 'LocationManager' and 'LocationListener'
15        //To get users location
16        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
17
18        //To get users permissions and updates about device movement
19        locationListener = new LocationListener() {
20            @Override
21            public void onLocationChanged(Location location) {
22                Log.i("Location:", location.toString());
23            }
24
25            @Override
26            public void onStatusChanged(String provider, int status, Bundle extras) {
27
28            }
29
30            @Override
31            public void onProviderEnabled(String provider) {
32
33            }
34
35            @Override
36            public void onProviderDisabled(String provider) {
37
38            }
39        };
40
41        //STEP 2: Create a Pop up message that ask the user to access his location
42
43        //The following if statement will execute when the user opens the app for the first time, which means he has
44        //not provided permission for anything yet, in which case the we will go ahead and ask for his permission.
45        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
46            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
47        } 
48        //user has already granted you permission so just go ahead and get updates. In this case the code will not continue to STEP 3.
49      	else {
50            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
51        }
52    }
53
54
55    //STEP 3: Respond accordingly to whether user granted permission or not
56    @Override
57    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
58        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
59
60        //Check if the user granted permission in step 2
61        if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
62
63            //If Yes, then go ahead and grab the users location user 'LocationManager' and 'LocationListener'
64            if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
65                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
66            }
67
68        }
69    }
70}