是否可以向库请求运行时权限?
Is it possible to ask for runtime permissions from a library?
我们制作了一些客户在他们的应用程序中实施的库,现在我们希望更新它们以支持 Android 6.0 新权限模型。
我的问题是,是否有任何方法可以在运行时从我们的库(主要是静态 类)请求危险权限,而不是让客户端在使用我们的库之前请求这些权限。
我一直在弄乱它,但对我来说这似乎是不可能的,看起来它必须由我们没有的 Activity 制成。
我说得对吗?有什么办法吗?
提前致谢。
looks like it has to be made from an Activity which we do not have.
正确。这是因为权限请求逻辑在很大程度上依赖于 startActivityForResult()
和 onActivityResult()
,它们被包装以处理权限请求。
此外,请求权限需要与整个应用程序流程紧密相关,UI-less 库将无法知道此时尝试请求权限是否合适.
欢迎您通过ContextCompat.checkSelfPermission()
检查您是否有权限,因为没有UI。因此,您可以在库的入口点使用它来检查您是否具有完成工作所需的权限,而不是让 SecurityException
或其他任何事情发生。
首先我想告诉你 运行 时间权限检查到 Android M,在 运行 时间如果你想通过用户授予权限,你需要一个用户交互权限对话框始终在 activity 或片段(在主线程上)上打开。
所以你可以为你的功能创建一个方法,参数是
public void yourMethod(Activty activity){
//条件申请权限检查如下
if (ContextCompat.checkSelfPermission(activity,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
Manifest.permission.READ_CONTACTS)) {
// Show an expanation 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.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
}
// Now you can handle call back in your activity through overriding the method // onRequestPermissionsResult this method must be written in your activity class
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
yourMethod(YourActivity.this);
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
// you can also call your method here but it may be create infinite loop if user deny run time permission
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
Activity 对象也通过创建参数构造获得。
为方法创建接口。
Google android 运行时权限的官方库 java 和 kotlin
库名称:EasyPermissions
我们制作了一些客户在他们的应用程序中实施的库,现在我们希望更新它们以支持 Android 6.0 新权限模型。
我的问题是,是否有任何方法可以在运行时从我们的库(主要是静态 类)请求危险权限,而不是让客户端在使用我们的库之前请求这些权限。
我一直在弄乱它,但对我来说这似乎是不可能的,看起来它必须由我们没有的 Activity 制成。
我说得对吗?有什么办法吗?
提前致谢。
looks like it has to be made from an Activity which we do not have.
正确。这是因为权限请求逻辑在很大程度上依赖于 startActivityForResult()
和 onActivityResult()
,它们被包装以处理权限请求。
此外,请求权限需要与整个应用程序流程紧密相关,UI-less 库将无法知道此时尝试请求权限是否合适.
欢迎您通过ContextCompat.checkSelfPermission()
检查您是否有权限,因为没有UI。因此,您可以在库的入口点使用它来检查您是否具有完成工作所需的权限,而不是让 SecurityException
或其他任何事情发生。
首先我想告诉你 运行 时间权限检查到 Android M,在 运行 时间如果你想通过用户授予权限,你需要一个用户交互权限对话框始终在 activity 或片段(在主线程上)上打开。
所以你可以为你的功能创建一个方法,参数是
public void yourMethod(Activty activity){
//条件申请权限检查如下
if (ContextCompat.checkSelfPermission(activity,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
Manifest.permission.READ_CONTACTS)) {
// Show an expanation 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.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
}
// Now you can handle call back in your activity through overriding the method // onRequestPermissionsResult this method must be written in your activity class
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
yourMethod(YourActivity.this);
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
// you can also call your method here but it may be create infinite loop if user deny run time permission
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
Activity 对象也通过创建参数构造获得。 为方法创建接口。
Google android 运行时权限的官方库 java 和 kotlin
库名称:EasyPermissions