Android 权限:用户按下 "Allow" 后执行任务
Android permissions: Perform task after user pressed "Allow"
我想知道当用户按下 "Allow" 按钮访问联系人详细信息/访问日历等时,是否有一种方法可以识别事件,
我知道有一种方法可以通过 ActivityCompat.requestPermissions 请求权限,但是有没有一种方法可以在用户授予权限后立即执行操作?
调用 requestPermissions()
请求权限。您在 onRequestPermissionsResult()
中找到结果,并可以确定他们是否授予了权限。从您的应用程序代码的角度来看,那是 "right after"。
首先定义变量:
public static int MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1;
请求权限使用:
if (ActivityCompat.checkSelfPermission(this,
android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
android.Manifest.permission.ACCESS_FINE_LOCATION)) {
// 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.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
现在使用以下方法获取结果:
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION : {
// 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.
Toast.makeText(getApplicationContext(), "Permission granted", Toast.LENGTH_SHORT).show();
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(getApplicationContext(), "Permission denied", Toast.LENGTH_SHORT).show();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
片段
如果您在 fragment
中尝试此代码,请更改
checkSelfPermission()
到
ActivityCompact.checkSelfPermission()
并更改
ActivityCompat.requestPermissions()
到
requestPermissions()
权限结果的处理(允许或拒绝)与activity相同。
有关更完整的示例,请参阅此
我为此目的使用了这段代码。
public boolean isPermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.CAMERA)
== PackageManager.PERMISSION_GRANTED) {
Log.v(TAG, "Permission is granted");
return true;
} else {
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.CAMERA
}, 1);
return false;
}
} else { //permission is automatically granted on sdk<23 upon installation
Log.v(TAG, "Permission is granted");
return true;
}
}
然后您可以致电:
if(isPermissionGranted())
{
// do your stuff
}
我想知道当用户按下 "Allow" 按钮访问联系人详细信息/访问日历等时,是否有一种方法可以识别事件,
我知道有一种方法可以通过 ActivityCompat.requestPermissions 请求权限,但是有没有一种方法可以在用户授予权限后立即执行操作?
调用 requestPermissions()
请求权限。您在 onRequestPermissionsResult()
中找到结果,并可以确定他们是否授予了权限。从您的应用程序代码的角度来看,那是 "right after"。
首先定义变量:
public static int MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1;
请求权限使用:
if (ActivityCompat.checkSelfPermission(this,
android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
android.Manifest.permission.ACCESS_FINE_LOCATION)) {
// 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.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
现在使用以下方法获取结果:
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION : {
// 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.
Toast.makeText(getApplicationContext(), "Permission granted", Toast.LENGTH_SHORT).show();
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(getApplicationContext(), "Permission denied", Toast.LENGTH_SHORT).show();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
片段
如果您在 fragment
中尝试此代码,请更改
checkSelfPermission()
到
ActivityCompact.checkSelfPermission()
并更改
ActivityCompat.requestPermissions()
到
requestPermissions()
权限结果的处理(允许或拒绝)与activity相同。
有关更完整的示例,请参阅此
我为此目的使用了这段代码。
public boolean isPermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.CAMERA)
== PackageManager.PERMISSION_GRANTED) {
Log.v(TAG, "Permission is granted");
return true;
} else {
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.CAMERA
}, 1);
return false;
}
} else { //permission is automatically granted on sdk<23 upon installation
Log.v(TAG, "Permission is granted");
return true;
}
}
然后您可以致电:
if(isPermissionGranted())
{
// do your stuff
}