Java 在 Android 应用程序中从用户获取位置权限
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44370162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Location Permissions from User in Android Application
提问by Antonio Gschossmann
采纳答案by Haresh Chhelana
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.
}
回答by Abhishek Charismatic
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 :
如果用户接受一次,那么您的应用程序将记住它,您将不再需要发送此 DialogBox。请注意,如果他决定,用户可以稍后禁用它。然后在请求位置之前,您必须测试是否仍然授予权限:
public boolean checkLocationPermission()
{
String permission = "android.permission.ACCESS_FINE_LOCATION";
int res = this.checkCallingOrSelfPermission(permission);
return (res == PackageManager.PERMISSION_GRANTED);
}
回答by Rajesh Bajaj
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);
}
}
}