如果有人知道怎么做,请回答。
原文由 Antonio Gschossmann 发布,翻译遵循 CC BY-SA 4.0 许可协议
如果有人知道怎么做,请回答。
原文由 Antonio Gschossmann 发布,翻译遵循 CC BY-SA 4.0 许可协议
你可以这样做
在清单中:
<uses-feature android:name="android.hardware.location.gps" />
在您想要此权限请求的活动中:
ActivityCompat.requestPermissions(this,new String[]
{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
然后使用此功能获取用户答案:
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
}
}
如果用户接受一次,那么您的应用程序将记住它,您将不再需要发送此 DialogBox。请注意,如果用户决定,他可以稍后将其禁用。然后在请求位置之前,您必须测试权限是否仍然被授予:
public boolean checkLocationPermission()
{
String permission = "android.permission.ACCESS_FINE_LOCATION";
int res = this.checkCallingOrSelfPermission(permission);
return (res == PackageManager.PERMISSION_GRANTED);
}
原文由 Abhishek Charismatic 发布,翻译遵循 CC BY-SA 3.0 许可协议
15 回答8.4k 阅读
8 回答6.2k 阅读
1 回答4k 阅读✓ 已解决
3 回答6k 阅读
3 回答2.2k 阅读✓ 已解决
2 回答3.1k 阅读
2 回答3.8k 阅读
在运行时请求权限以使用设备当前位置,如下所示: