Android权限:在用户按下“允许"后,执行任务 [英] Android permissions: Perform task after user pressed "Allow"

查看:84
本文介绍了Android权限:在用户按下“允许"后,执行任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以在用户按下允许"按钮以进行联系方式访问/日历访问等时识别事件,

I would like to know if there is a way we can identify the event when the user has pressed "Allow" button for contact details access/ calendar access etc.,

我知道可以通过ActivityCompat.requestPermissions来请求权限,但是有没有一种方法可以在用户授予权限后立即执行操作?

I know there is a way to ask for permissions with the ActivityCompat.requestPermissions but is there a way to perform an action right after the user grants a permission?

推荐答案

首先定义变量:

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.
    }
}

现在使用以下命令捕获结果:

now catch the result using:

     @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 中尝试此代码,请更改

if you are trying this code in a fragment, change

checkSelfPermission()

ActivityCompact.checkSelfPermission()

ActivityCompact.checkSelfPermission()

并同时更改

ActivityCompat.requestPermissions()

requestPermissions()

requestPermissions()

权限结果(允许或拒绝)的处理与活动相同.

Handling of Permission Result (Allow or Deny) are same as activity.

有关更完整的示例,请参见此在此处回答

For a More Complete Example See This Answer Here

这篇关于Android权限:在用户按下“允许"后,执行任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆