Android 6+权限概念和位置策略
Android 6+ Permission concept and Location Strategies
Google 如何看待这个带有 Android 6+ 权限的奇怪概念,用户必须接受 Location 策略的权限。我的详细意思是:新的权限概念应该在用户想要使用应用程序中的特殊功能的那一刻向他显示授予权限弹出窗口。这应该如何与 locationManager.requestLocationUpdates(...)
一起使用? manual 表示
You might want to start listening for location updates as soon as your
application starts, or only after users activate a certain feature.
因此,如果我在应用程序启动时显示权限弹出窗口,则此权限概念将毫无意义,因为用户必须在不知道为什么的情况下接受权限。当我在他按下位置按钮时执行 locationManager.requestLocationUpdates(...)
时,据我所知,位置管理器的速度不够快,无法获得正确的位置(很难测试,所以我不确定)。
使用位置管理器时 Android 6+ 权限的正确实施概念是什么?
最好在启动时向您的用户请求位置权限。由于每次安装只需要请求一次许可,所以应该不会太烦人。如果用户允许您的许可,您无需担心其他任何事情,用户可以继续使用您的应用程序。但如果他拒绝,最好向他显示一个弹出对话框,解释为什么您的应用需要此权限才能继续。如果他仍然拒绝,您可以选择强制他离开应用程序或限制您之前请求许可的功能。
看到这个official doc
在启动时询问权限可以帮助您禁用与特定权限相关的任何功能。如果需要,您可以在真正需要时请求许可并检查用户响应。这样做的三种基本方法是:
checkSelfPermission
(确定您是否已被授予特定权限。)
shouldShowRequestPermissionRationale
(获取您是否应该显示 UI 以及请求权限的理由。仅当您没有权限并且请求权限的上下文没有时才应该这样做没有清楚地向用户传达授予此权限的好处。)
requestPermissions
(请求授予此应用程序的权限。必须在您的清单中请求这些权限。)
结合这三种方法,您可以创建一个工作流来处理动态请求权限,例如:
public boolean checkPermission(int requestCode, int permissionExplanation, OnRequestPermissionCallback listener, String... permissions) {
boolean permissionGranted = true;
for (String permission: permissions) {
if(ActivityCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED){
permissionGranted = false;
break;
}
}
if (!permissionGranted) {
// Should we show an explanation?
boolean shouldShowRequestPermissionRationale = true;
for (String permission: permissions) {
if(!ActivityCompat.shouldShowRequestPermissionRationale(this, permission)){
shouldShowRequestPermissionRationale = false;
break;
}
}
if (shouldShowRequestPermissionRationale) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
ActivityUtils.showAlertInfoMessage(this, getString(permissionExplanation), getString(R.string.dialog_information), v -> {
ActivityCompat.requestPermissions(this, permissions, requestCode);
}, false);
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this, permissions, requestCode);
}
return false;
}
return true;
}
您应该检查 onRequestPermissionsResult
回调方法中授予的用户权限(注意:此方法在 activity 中被调用,因为如果您使用 ActivityCompat
中的三种方法想要从 Fragment
处理 onRequestPermissionsResult
你需要使用 FragmentCompat
而你的片段必须实现包含抽象方法 onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
的 FragmentCompat.OnRequestPermissionsResultCallback
,看看这个link: https://developer.android.com/reference/android/support/v13/app/FragmentCompat.html) 了解更多详情。
Google 如何看待这个带有 Android 6+ 权限的奇怪概念,用户必须接受 Location 策略的权限。我的详细意思是:新的权限概念应该在用户想要使用应用程序中的特殊功能的那一刻向他显示授予权限弹出窗口。这应该如何与 locationManager.requestLocationUpdates(...)
一起使用? manual 表示
You might want to start listening for location updates as soon as your application starts, or only after users activate a certain feature.
因此,如果我在应用程序启动时显示权限弹出窗口,则此权限概念将毫无意义,因为用户必须在不知道为什么的情况下接受权限。当我在他按下位置按钮时执行 locationManager.requestLocationUpdates(...)
时,据我所知,位置管理器的速度不够快,无法获得正确的位置(很难测试,所以我不确定)。
使用位置管理器时 Android 6+ 权限的正确实施概念是什么?
最好在启动时向您的用户请求位置权限。由于每次安装只需要请求一次许可,所以应该不会太烦人。如果用户允许您的许可,您无需担心其他任何事情,用户可以继续使用您的应用程序。但如果他拒绝,最好向他显示一个弹出对话框,解释为什么您的应用需要此权限才能继续。如果他仍然拒绝,您可以选择强制他离开应用程序或限制您之前请求许可的功能。
看到这个official doc
在启动时询问权限可以帮助您禁用与特定权限相关的任何功能。如果需要,您可以在真正需要时请求许可并检查用户响应。这样做的三种基本方法是:
checkSelfPermission
(确定您是否已被授予特定权限。)shouldShowRequestPermissionRationale
(获取您是否应该显示 UI 以及请求权限的理由。仅当您没有权限并且请求权限的上下文没有时才应该这样做没有清楚地向用户传达授予此权限的好处。)requestPermissions
(请求授予此应用程序的权限。必须在您的清单中请求这些权限。)
结合这三种方法,您可以创建一个工作流来处理动态请求权限,例如:
public boolean checkPermission(int requestCode, int permissionExplanation, OnRequestPermissionCallback listener, String... permissions) {
boolean permissionGranted = true;
for (String permission: permissions) {
if(ActivityCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED){
permissionGranted = false;
break;
}
}
if (!permissionGranted) {
// Should we show an explanation?
boolean shouldShowRequestPermissionRationale = true;
for (String permission: permissions) {
if(!ActivityCompat.shouldShowRequestPermissionRationale(this, permission)){
shouldShowRequestPermissionRationale = false;
break;
}
}
if (shouldShowRequestPermissionRationale) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
ActivityUtils.showAlertInfoMessage(this, getString(permissionExplanation), getString(R.string.dialog_information), v -> {
ActivityCompat.requestPermissions(this, permissions, requestCode);
}, false);
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this, permissions, requestCode);
}
return false;
}
return true;
}
您应该检查 onRequestPermissionsResult
回调方法中授予的用户权限(注意:此方法在 activity 中被调用,因为如果您使用 ActivityCompat
中的三种方法想要从 Fragment
处理 onRequestPermissionsResult
你需要使用 FragmentCompat
而你的片段必须实现包含抽象方法 onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
的 FragmentCompat.OnRequestPermissionsResultCallback
,看看这个link: https://developer.android.com/reference/android/support/v13/app/FragmentCompat.html) 了解更多详情。