This question already has an answer here:
- How to request Location Permission at runtime 4 answers
I´m trying to get the users actual location with the typicaly permissions box: like this:
If anyone knows how to make it , pleas answer.
This question already has an answer here:
I´m trying to get the users actual location with the typicaly permissions box: like this:
If anyone knows how to make it , pleas answer.
Ask for permission at runtime to use device current location as below :
if (ActivityCompat.checkSelfPermission(YourActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(YourActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(YourActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
return;
}else{
// Write you code here if permission already given.
}
You can do it like this
In Manifest:
<uses-feature android:name="android.hardware.location.gps" />
In Activity where you want this permission request:
ActivityCompat.requestPermissions(this,new String[]
{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
Then get the user answer by using this function :
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
If the user accept it once, then your app will remember it and you won't need to send this DialogBox anymore. Note that the user could disable it later if he decided to. Then before requesting the location, you would have to test if the permission is still granted :
public boolean checkLocationPermission()
{
String permission = "android.permission.ACCESS_FINE_LOCATION";
int res = this.checkCallingOrSelfPermission(permission);
return (res == PackageManager.PERMISSION_GRANTED);
}
First, you need to write a class where you will define a method for granting and checking user location permission.
public class CheckPermission {
// CHECK FOR LOCATION PERMISSION
public static boolean checkPermission(Activity activity){
int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION);
if (result == PackageManager.PERMISSION_GRANTED){
return true;
} else {
return false;
}
}
//REQUEST FOR PERMISSSION
public static void requestPermission(Activity activity, final int code){
if (ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.ACCESS_FINE_LOCATION)){
Toast.makeText(activity,"GPS permission allows us to access location data. Please allow in App Settings for additional functionality.",Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},code);
}
}
}
Now you can call above two static methods to checking or granting permissions in your activity.
public class LocationActivity extends AppCompatActivity {
private GPSTracker mytracker;
private Location myLocation;
private double latitude=0.0d,longitude=0.0d;
public final static int TAG_PERMISSION_CODE=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
if(!CheckPermission.checkPermission(LocationActivity.this)) {
CheckPermission.requestPermission(SplashScreenActivity.this,TAG_PERMISSION_CODE);
}
}
}