how to ask user for his location in android
Java
public class MainActivity extends AppCompatActivity {
//'LocationManager' manages location tracking
/*'LocationListener' gives us updates about whenever the device moves, it gives us updates on all those */
LocationManager locationManager;
LocationListener locationListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//STEP 1: Declare and Initialize 'LocationManager' and 'LocationListener'
//To get users location
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
//To get users permissions and updates about device movement
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.i("Location:", location.toString());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
//STEP 2: Create a Pop up message that ask the user to access his location
//The following if statement will execute when the user opens the app for the first time, which means he has
//not provided permission for anything yet, in which case the we will go ahead and ask for his permission.
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
//user has already granted you permission so just go ahead and get updates. In this case the code will not continue to STEP 3.
else {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
}
//STEP 3: Respond accordingly to whether user granted permission or not
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//Check if the user granted permission in step 2
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//If Yes, then go ahead and grab the users location user 'LocationManager' and 'LocationListener'
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
}
}
}
Also in Java:
- Title
- Spigot API inventory close
- Category
- Java
- Title
- java == vs equals
- Category
- Java
- Title
- override java
- Category
- Java
- Title
- how to create an array in java
- Category
- Java
- Title
- java 8 find min value in array
- Category
- Java
- Title
- how to loop trough an object java script
- Category
- Java
- Title
- iterate through hashtable java
- Category
- Java
- Title
- read csv in java in spring
- Category
- Java
- Title
- how to create a circle in java
- Category
- Java
- Title
- read integer input java
- Category
- Java
- Title
- connect 2 package in android
- Category
- Java
- Title
- java script to detect the crome browser
- Category
- Java
- Title
- java how to initialize an array
- Category
- Java
- Title
- how to print the map in java
- Category
- Java
- Title
- java creare costante
- Category
- Java
- Title
- print in java
- Category
- Java
- Title
- python vs java
- Category
- Java
- Title
- java min function
- Category
- Java
- Title
- remove space string java
- Category
- Java
- Title
- capitalize string java
- Category
- Java
- Title
- java sql question mark
- Category
- Java
- Title
- official java website
- Category
- Java
- Title
- fibonacci sequence in java recursion
- Category
- Java
- Title
- java create directory
- Category
- Java
- Title
- what is static setter and getter examples in java
- Category
- Java
- Title
- java polymorphism
- Category
- Java
- Title
- set iteration java
- Category
- Java
- Title
- convert string to int java
- Category
- Java
- Title
- null pointer exception java
- Category
- Java
- Title
- primitive data types in java
- Category
- Java
- Title
- change the value in a hashtable java
- Category
- Java
- Title
- encode file to utf-8 in java
- Category
- Java
- Title
- height constraint layout guideline
- Category
- Java
- Title
- java http client eviction
- Category
- Java
- Title
- how to make a char uppercase in java
- Category
- Java
- Title
- import java.util.hashset
- Category
- Java
- Title
- if statement java
- Category
- Java
- Title
- biginteger in java
- Category
- Java
- Title
- IN APPLet how to disable a button in java
- Category
- Java
- Title
- sort elements with sortedset
- Category
- Java
- Title
- reading string after double in java
- Category
- Java
- Title
- hide elements android
- Category
- Java
- Title
- jackson ignore values if empty
- Category
- Java
- Title
- Removing DOM nodes when traversing a NodeList
- Category
- Java
- Title
- java command line arguments
- Category
- Java
- Title
- writing to a text file java
- Category
- Java
- Title
- find a substring in a string java
- Category
- Java
- Title
- getcolor deprecated
- Category
- Java
- Title
- android how to split string
- Category
- Java
- Title
- how to initialize an array in java
- Category
- Java
- Title
- java catch multiple exceptions
- Category
- Java
- Title
- how to check the end of a string java
- Category
- Java
- Title
- java replace all xml decoding
- Category
- Java
- Title
- stack overflow recyclerview
- Category
- Java
- Title
- how to count an replace substring string in java
- Category
- Java
- Title
- org.mapstruct maven dependency
- Category
- Java
- Title
- inline foreach java
- Category
- Java
- Title
- java how to get current date
- Category
- Java
- Title
- java mockito any string import
- Category
- Java
- Title
- how to input in java
- Category
- Java
- Title
- check if string is null or empty java
- Category
- Java
- Title
- java create unmodifiable list
- Category
- Java
- Title
- android java how to stop users fromgoing back too much
- Category
- Java
- Title
- java delay
- Category
- Java
- Title
- java date time
- Category
- Java
- Title
- initiralizing an arraylist java
- Category
- Java
- Title
- write file from a specific location in java
- Category
- Java
- Title
- java script to detect and launch all browsers
- Category
- Java
- Title
- if en une seul ligne java
- Category
- Java
- Title
- how to iterate hashmap in java
- Category
- Java
- Title
- java methods
- Category
- Java
- Title
- how to skip a line in java
- Category
- Java
- Title
- context error in android studio
- Category
- Java
- Title
- how to iterate hashset in java 8
- Category
- Java
- Title
- 2d arrays | java
- Category
- Java
- Title
- how to use spring in java
- Category
- Java
- Title
- java 8 random string generator
- Category
- Java
- Title
- get world by name bukkit
- Category
- Java
- Title
- compareto method java
- Category
- Java
- Title
- change fab image programatically
- Category
- Java
- Title
- how to use an abstract class in java
- Category
- Java
- Title
- what is graphics default color java
- Category
- Java
- Title
- how to create a linked list in java
- Category
- Java
- Title
- creating a program about user asked to enter a number and enters 0 to stop, then calculates the sum of numbers entered, the average, the length of positive numbers and negative numbers entered by the user in java
- Category
- Java
- Title
- java random integer
- Category
- Java
- Title
- sqrt in java
- Category
- Java
- Title
- java hashmap size
- Category
- Java
- Title
- processing library cassette
- Category
- Java
- Title
- java get size of array
- Category
- Java
- Title
- cannot lock java compile cache as it has already been locked by this process
- Category
- Java
- Title
- java io
- Category
- Java
- Title
- Java script code for add
- Category
- Java
- Title
- double round java integer
- Category
- Java
- Title
- java comments
- Category
- Java
- Title
- write in file java
- Category
- Java
- Title
- import scanner
- Category
- Java
- Title
- create color object java
- Category
- Java
- Title
- rock paper scissors java
- Category
- Java
- Title
- remove item from arraylist in java
- Category
- Java
- Title
- jbutton open jframe java
- Category
- Java