当前位置: 首页 > 知识库问答 >
问题:

用户Android M在应用程序中授予的权限

苏鸿羽
2023-03-14

我想在我的应用程序。如果已授予权限在android中不显示对话框这是我的代码

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.button:
                requestContactsPermissions();
            break;
        case R.id.button1:
          //  requestCameraPermissions();
            Toast.makeText(mcontext, "Hello", Toast.LENGTH_LONG).show();
            break;
    }

}

private void requestContactsPermissions() {

     boolean requestPermission = PersimissionTest.requestContactsPermissions(this);

    if (requestPermission == true) {
        ActivityCompat.requestPermissions(this, PERMISSIONS_CONTACTS, REQUEST_ID);

    } else {
       ActivityCompat.requestPermissions(this, PERMISSIONS_CONTACTS, REQUEST_ID);

    }

}

@override public void onRequestPermissionsResult(int requestCode,String permissions[],int[]grantResults){

    if (requestCode == REQUEST_ID) {

        if (PersimissionTest.verifyPermissions(grantResults)) {

            Toast.makeText(mcontext, "Contacts permissions granted.", Toast.LENGTH_LONG).show();

        } else {
            Toast.makeText(mcontext, "Contacts permissions were not granted.", Toast.LENGTH_LONG).show();
        }

    } else {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

共有2个答案

柳项明
2023-03-14

像这样请求权限,如果权限已经被授予而不是插入联系人,否则它将请求权限

private void insertDummyContactWrapper(String mStringPhoneContactName, String mStringPhoneContactNumber) {
    boolean hasWriteContactsPermission = ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.WRITE_CONTACTS);
    if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(SwipableHomeActivity.this, new String[]{android.Manifest.permission.WRITE_CONTACTS}, CONSTANTS.MY_PERMISSIONS_REQUEST_WRITE_CONTACTS);
        return;
    }
    //Insert Contact
}

获取权限结果如下所示

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case CONSTANTS.MY_PERMISSIONS_REQUEST_WRITE_CONTACTS:

            if (grantResults != null && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                ReadPhoneContacts(getApplicationContext(), mStringPhoneContactName, mStringPhoneContactNumber);
            } else {
                Toast.makeText(getApplicationContext(), "Permission Denied", Toast.LENGTH_SHORT).show();
            }
            break;
    }
}

检查我在你的问题中实现的这个

 public static boolean requestContactsPermissions(Activity activity) {
    boolean requestPermission;
    if (ContextCompat.checkSelfPermission(activity, android.Manifest.permission.WRITE_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
        requestPermission = true;
    }else{
        requestPermission = false;
    }
    return requestPermission;
}

public static boolean requestCameraPermissions(Activity activity) {
    boolean requestPermission;
    if (ContextCompat.checkSelfPermission(activity, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
        requestPermission = true;
    } else {
        requestPermission = false;
    }
    return requestPermission;
}

使用以下命令更改类

public class MainActivity extends AppCompatActivity implements
    OnRequestPermissionsResultCallback, OnClickListener{

public static final String TAG = "PermissionActivity";

private View mLayout;
Button mButtonCheckPermission = null;

private static final int REQUEST_CONTACTS = 1;
private static String[] PERMISSIONS_CONTACT = {Manifest.permission.READ_CONTACTS,
        Manifest.permission.WRITE_CONTACTS};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initView();
}

private void initView() {
    mButtonCheckPermission = (Button) findViewById(R.id.button_check_permissions);
    mButtonCheckPermission.setOnClickListener(this);

    mLayout = findViewById(R.id.sample_main_layout);

}

@Override
public void onClick(View v) {

    switch (v.getId()) {
        case R.id.button_check_permissions:
            requestContactsPermissions();
            break;

        default:
            break;
    }

}

private void requestContactsPermissions() {

    boolean requestPermission = PermissionUtil.requestContactsPermissions(this);

    if (requestPermission == true){

        Log.i(TAG,
                "Displaying contacts permission rationale to provide additional context.");

        // Display a SnackBar with an explanation and a button to trigger the request.

        if (ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.WRITE_CONTACTS) != PackageManager.PERMISSION_GRANTED) {

            Snackbar.make(mLayout, R.string.permission_contacts_rationale,
                    Snackbar.LENGTH_INDEFINITE)
                    .setAction(R.string.ok, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            ActivityCompat.requestPermissions(MainActivity.this, PERMISSIONS_CONTACT,
                                    REQUEST_CONTACTS);
                        }
                    })
                    .show();
        }else{
            Snackbar.make(mLayout, R.string.permision_available_contacts,
                    Snackbar.LENGTH_LONG)
                    .show();
        }
    }
    else {

        ActivityCompat.requestPermissions(this, PERMISSIONS_CONTACT, REQUEST_CONTACTS);

    }

}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {

    if (requestCode == REQUEST_CONTACTS) {
        Log.i(TAG, "Received response for contact permissions request.");

        // We have requested multiple permissions for contacts, so all of them need to be
        // checked.
        if (PermissionUtil.verifyPermissions(grantResults)) {
            // All required permissions have been granted, display contacts fragment.
            Snackbar.make(mLayout, R.string.permision_available_contacts,
                    Snackbar.LENGTH_LONG)
                    .show();
        } else {
            Log.i(TAG, "Contacts permissions were NOT granted.");
            Snackbar.make(mLayout, R.string.permissions_not_granted,
                    Snackbar.LENGTH_LONG)
                    .show();
        }

    } else {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}}
令狐辉
2023-03-14

下面的代码检查应用程序是否具有读取用户联系人的权限,并在必要时请求该权限:

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
            Manifest.permission.READ_CONTACTS)
    != PackageManager.PERMISSION_GRANTED) {

    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
        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(thisActivity,
            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.
    }
}
else
{
    // Permission already granted ... This is where you can continue your further business logic...
}

下面是回调方法:

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

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

详解访问此处(安卓官方页面)或此处(Tutsplus教程):

 类似资料:
  • 在Ora11g中,我使用以下方式向用户授予所有权限。 但在ORA12C中,当我执行grant privileges时,我得到以下错误。 从命令-授予创建会话,向xx授予任何权限错误报告-ORA-00604:递归SQL级别1 ORA-20997发生错误:“授予任何权限”授予不允许的ORA-06512:在“RDSADMIN.RDSADMIN”处,第79行ORA-06512:在第2 00604行。000

  • 我们公司正在开发一个基于Azure组件的系统和一个连接到Azure的客户端桌面应用程序。Azure组件是由我们的设置代码通过Azure API和Azure部署自动化自动部署的。正在部署的组件之一是我们在Azure Active Directory中注册的Web App/API。我们的部署代码通过Azure API在Azure中创建这个应用程序,并在Azure中为这个应用程序设置“所需的权限”。所需

  • 问题内容: 我在注册后使用以下方法进行程序化登录 用户已通过身份验证,但始终具有ROLE_ANONYMOUS我不知道为什么?有任何想法吗 ? 问题答案: 这种现象看起来很奇怪。Javi建议手动将安全性上下文持久化到会话中,但是应该由Spring Security的自动完成。 一个可能的原因,我可以想像是在你的登记处理的页面。 禁用指定URL的所有安全过滤器。如你所见,它可能会干扰Spring Se

  • 我正在尝试制作一个使用数据库的java应用程序。我已经下载了db2并创建了一个用户'student'。我的程序中有以下几行: 显然,我几乎不知道我在做什么,我不知道如何给予“学生”必要的特权。也就是说,我不知道什么用户可以给他特权,因为我从来没有创建过另一个用户。我看到有人提到过'db2admin'(我使用的是windows),但我不知道该如何处理这段信息。我不知道如何检查现有用户,也不知道对db

  • 我希望这个应用程序能够访问Graph API上的mail.read作用域,仅供特定的用户组使用。 我设法让整个组织都能进入。(点击Azure门户>Azure Active Directory>应用注册>MyApp>设置>权限中的“授予权限”按钮)。如何将这些权利限制在特定的用户群体,而不必提示任何个人同意?

  • 已试过 获取 错误1064(42000):您的SQL语法中有错误;查看与您的MySQL服务器版本相对应的手册,了解第1行“标识由'root'WITH GRANT option”附近使用的正确语法。 注意:在以前的版本中尝试时同样有效。 也试过 获取 错误1410(42000):不允许创建具有授权的用户 MySQL(8.0.11.0)用户名/密码是root/root。