第一次启动应用程序时,请避免使用权限对话框

时间:2018-04-19 08:51:21

标签: android permissions

我收到谷歌的通知,说"确保在启动时触发任何权限请求之前显示权限上下文消息"

但我刚刚在manifest.xml中声明了权限,并且在启动时会自动显示权限请求,即使我没有开始检查/请求危险权限。 在其他一些设备上它不会出现。

根据谷歌的测试视频,我了解到对话框显示在他们的设备上。我已添加了有关危险权限的上下文,但无法在启动时的对话框之前找到显示它的方法。

所以我的问题是:关于它是否会显示权限对话框的规则是什么? 以及如何确保在启动时触发任何权限请求之前显示权限上下文消息"

2 个答案:

答案 0 :(得分:0)

  

在MainActivity类声明中:

private static String[] PERMISSIONS = {Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_CONTACTS, Manifest.permission.SEND_SMS, Manifest.permission.GET_ACCOUNTS, Manifest.permission.PROCESS_OUTGOING_CALLS};
public int ALL_PERMISSIONS = 0;
public boolean CheckNoPermission = false;
  

在Marshmallow设备上的运行时请求权限是强制性的,此代码应该在onCreate方法中

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
{
     ALL_PERMISSIONS  = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE);
     ALL_PERMISSIONS  = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_CONTACTS);
     ALL_PERMISSIONS  = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.SEND_SMS);
     ALL_PERMISSIONS  = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.GET_ACCOUNTS);
     ALL_PERMISSIONS  = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.PROCESS_OUTGOING_CALLS);
}
else
{
    Log.d(TAG, "ITS MARSHMALLOW OR LATER ANDROID VERSION DETECTED.......\n");
    Log.d(TAG, "REQUESTING USER PERMISSIONS..... waiting for user to grant....\n");

    for(int x = 0; x < PERMISSIONS.length; x++)
    {
         String ThisPermission = PERMISSIONS[x];

         if (checkSelfPermission(ThisPermission) != PackageManager.PERMISSION_GRANTED)
         {
               CheckNoPermission = true;
               break;
         }
    }
    if(CheckNoPermission)
    {

         ActivityCompat.requestPermissions(MainActivity.this, PERMISSIONS, ALL_PERMISSIONS);
    }
    else
    {
         // you already have permissions
    }
}
  

在onCreate方法之外,但在MainActivity类中,另一种检查用户是否允许或拒绝权限的方法:

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
{
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (grantResults.length == 5)
    {
        if(grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED && grantResults[2] == PackageManager.PERMISSION_GRANTED && grantResults[3] == PackageManager.PERMISSION_GRANTED && grantResults[4] == PackageManager.PERMISSION_GRANTED)
        {
           // all permissions granted
        }
   }
   else
   {
       // one of the permission is not granted
   }
}

享受.. :))

答案 1 :(得分:0)

假设您可以访问marshmallow设备上方的设备联系人详细信息,那时您需要获得该设备下方的权限,而无需获得许可。

以下方法获取设备联系人详细信息如果您的设备是棉花糖然后需要权限我添加权限检查,如

首先检查权限,例如为此制作自定义方法..

    /**
 * Return the current state of the permissions needed.
 */
private boolean checkPermissions() {
    int permissionState = ActivityCompat.checkSelfPermission(this,
            Manifest.permission.READ_CONTACTS);
    return permissionState == PackageManager.PERMISSION_GRANTED;
}

然后在向用户请求许可后如下..

   /**
 * this method request to permission asked.
 */
private void requestPermissions() {
    boolean shouldProvideRationale =
            ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.READ_CONTACTS);

    if (shouldProvideRationale) {
        Log.i(TAG, "Displaying permission rationale to provide additional context.");
        ActivityCompat.requestPermissions(ContactListActivity.this,
                new String[]{Manifest.permission.READ_CONTACTS},
                REQUEST_PERMISSIONS_REQUEST_CODE);

    } else {
        Log.i(TAG, "Requesting permission");
        // previously and checked "Never ask again".
        ActivityCompat.requestPermissions(ContactListActivity.this,
                new String[]{Manifest.permission.READ_CONTACTS},
                REQUEST_PERMISSIONS_REQUEST_CODE);
    }
}

然后制作获取联系方式的方法..

  /**
 * Get the contacts of the user and add each contact in the mContactList
 * And finally pass the mContactList to the mChipsInput
 */
private void getContactList() {
    Cursor phones = this.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

    // loop over all contacts
    if (phones != null) {
        while (phones.moveToNext()) {
            // get contact info
            String phoneNumber = null;
            String id = phones.getString(phones.getColumnIndex(ContactsContract.Contacts._ID));
            String name = phones.getString(phones.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            String avatarUriString = phones.getString(phones.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI));
            Uri avatarUri = null;
            if (avatarUriString != null)
                avatarUri = Uri.parse(avatarUriString);

            // get phone number
            if (Integer.parseInt(phones.getString(phones.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                Cursor pCur = this.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);

                while (pCur != null && pCur.moveToNext()) {
                    phoneNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                }

                pCur.close();

            }

            ContactChip contactChip = new ContactChip(id, avatarUri, name, phoneNumber);
            // add contact to the list
            mContactList.add(contactChip);
        }
        phones.close();
    }

    // pass contact list to chips input
    mChipsInput.setFilterableList(mContactList);
}

当你需要调用getContact详细信息方法时首先检查权限,如下所示..

        // get contact list
    if (!checkPermissions()) {
        requestPermissions();
    }
    else {
        getContactList();
    }

并将联系人权限添加到Android清单文件中,如下所示。

    <uses-permission android:name="android.permission.READ_CONTACTS"/>