Android 6.0(棉花糖)READ_CONTACTS权限允许在拒绝权限时读取联系人的姓名 [英] Android 6.0 (Marshmallow) READ_CONTACTS permission allows to read Contact's name when permission is denied

查看:158
本文介绍了Android 6.0(棉花糖)READ_CONTACTS权限允许在拒绝权限时读取联系人的姓名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查新权限模型的工作原理,因此在应用程序设置中我禁用了Contacts.然后我进入应用程序,尝试阅读Contacts,并且...行之有效:

I want to check how new permission model works so in app's settings I disable Contacts. Then I go to app and try to read Contacts and ... it kinda works:

try {
    Uri result = data.getData();
    int contentIdx;
    cursor = getContentResolver().query(result, null, null, null, null);
    contentIdx = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
    if(cursor.moveToFirst()) {
        content = cursor.getInt(contentIdx);
    }

    if(content > 0) {
        contentIdx = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
        if(cursor.moveToFirst()) {
            name = cursor.getString(contentIdx);
        }
        contentIdx = cursor.getColumnIndex(BaseColumns._ID);
        if(cursor.moveToFirst()) {
            content = cursor.getLong(contentIdx);
        }
        cursor = managedQuery(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[] { Phone.NUMBER }, Data.CONTACT_ID + "=?", new String[] { String.valueOf(content) }, null);
        if(cursor.moveToFirst()) {
            number = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));
        }
    }
} catch (Exception e) {
    //SecurityException
}

  • 我能够读取联系人的姓名
  • 当我尝试读取联系人的电话号码SecurityException
    • I'm able to read Contact's Name
    • when I try to read Contact's Number SecurityException is thrown
    • java.lang.SecurityException:权限拒绝:从pid = 20123读取com.android.providers.contacts.HtcContactsProvider2 uri content://com.android.contacts/data/phones,uid=10593需要android.permission.READ_CONTACTS或grantUriPermission()

      java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.HtcContactsProvider2 uri content://com.android.contacts/data/phones from pid=20123, uid=10593 requires android.permission.READ_CONTACTS, or grantUriPermission()

      那是为什么?

      相关内容:推荐答案

      Android棉花糖具有新的权限系统. 您需要在运行时请求权限. http://developer.android.com/training/permissions/requesting.html

      Android marshmallow have new permissions system. You need to request permissions at run time. http://developer.android.com/training/permissions/requesting.html.

      示例:

      @Override
      public void onClick(View v) {
          switch (v.getId()){
              case R.id.tv_contact:{
                  askForContactPermission();
                  break;
              }
          }
       }
      
      private void getContact(){
          Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
          startActivityForResult(intent, PICK_CONTACT);
      }
      
       public void askForContactPermission(){
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
              if (ContextCompat.checkSelfPermission(getActivity(),Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
      
                  // Should we show an explanation?
                  if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
                          Manifest.permission.READ_CONTACTS)) {
                      AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                      builder.setTitle("Contacts access needed");
                      builder.setPositiveButton(android.R.string.ok, null);
                      builder.setMessage("please confirm Contacts access");//TODO put real question
                      builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
                          @TargetApi(Build.VERSION_CODES.M)
                          @Override
                          public void onDismiss(DialogInterface dialog) {
                              requestPermissions(
                                      new String[]
                                              {Manifest.permission.READ_CONTACTS}
                                      , PERMISSION_REQUEST_CONTACT);
                          }
                      });
                      builder.show();
                      // 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(getActivity(),
                              new String[]{Manifest.permission.READ_CONTACTS},
                              PERMISSION_REQUEST_CONTACT);
      
                      // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
                      // app-defined int constant. The callback method gets the
                      // result of the request.
                  }
              }else{
                  getContact();
              }
          }
          else{
              getContact();
          }
      }
      
      @Override
      public void onRequestPermissionsResult(int requestCode,
                                             String permissions[], int[] grantResults) {
          switch (requestCode) {
              case PERMISSION_REQUEST_CONTACT: {
                  // If request is cancelled, the result arrays are empty.
                  if (grantResults.length > 0
                          && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                      getContact();
                      // permission was granted, yay! Do the
                      // contacts-related task you need to do.
      
                  } else {
                      ToastMaster.showMessage(getActivity(),"No permission for contacts");
                      // permission denied, boo! Disable the
                      // functionality that depends on this permission.
                  }
                  return;
              }
      
              // other 'case' lines to check for other
              // permissions this app might request
          }
      }
      

      这篇关于Android 6.0(棉花糖)READ_CONTACTS权限允许在拒绝权限时读取联系人的姓名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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