安卓权限: Perform task after user pressed "Allow"

标签 android android-permissions

我想知道当用户按下“允许”按钮以访问联系人详细信息/日历访问等时,我们是否可以识别事件,

我知道有办法通过 ActivityCompat.requestPermissions 请求权限,但有没有办法在用户授​​予权限后立即执行操作?

最佳答案

首先定义变量:

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

如果您在 fragment 中尝试此代码,请更改

checkSelfPermission()

ActivityCompact.checkSelfPermission()

还有变化

ActivityCompat.requestPermissions()

requestPermissions()

权限结果(允许或拒绝)的处理与 Activity 相同。

有关更完整的示例,请参阅 Answer Here

关于安卓权限: Perform task after user pressed "Allow",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41620466/

相关文章:

android - 以编程方式卸载应用程序

android - checkSelfPermission 方法在 targetSdkVersion 22 中不起作用

android - 如何从android URI获取绝对音频路径

Android 透明属性不适用于 Android 4.1.1

java - Android - View.requestLayout 在 OnLayoutChangeListener 中不起作用

java - 我收到 Auth Error : Authentication failure due to [java. io.FileNotFoundException: while implementation pusher-js in Android studio

android - 如何使禁用的微调器看起来不禁用?

android - Ionic android 请求位置许可

android - 为什么HMS PushKit需要android.permission.REQUEST_INSTALL_PACKAGES

android - 如何使用 adb grant 或 adb revoke?