所有Android版本中的联系*限

时间:2023-01-18 09:04:00

I am working on android application in which I need to ask a user for Contact permission before uploading contacts. Below is the code which I am using for Contacts Permission for Build.VERSION.SDK_INT >= Build.VERSION_CODES.M.

我正在开发Android应用程序,我需要在上传联系人之前询问用户的联系*限。下面是我用于Build.VERSION.SDK_INT> = Build.VERSION_CODES.M的联系*限的代码。

But the problem is I need to ask for the Contact Permission every time even below to Android M always. How to ask for Contact Permission which should work on all SDK Versions?

但问题是我需要每次都要求联系*限,甚至低于Android M。如何申请适用于所有SDK版本的联系*限?

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(this,Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {

                // Should we show an explanation?
                if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                        Manifest.permission.READ_CONTACTS)) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    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(this,
                            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();
        }
    }

    int PERMISSION_REQUEST_CONTACT = 1;

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case 1: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//                  getContact();
                    Toast.makeText(getApplicationContext(),"Permission is Done",Toast.LENGTH_LONG).show();
                    // permission was granted, yay! Do the
                    // contacts-related task you need to do.

                } else {
                    Toast.makeText(getApplicationContext(),"No permission for contacts",Toast.LENGTH_LONG).show();
                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                }
                return;
            }

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

1 个解决方案

#1


1  

Create a method for showing a permission request dialog, that always shows the dialog, but only calls the permission API on Android M and above:

创建一个显示权限请求对话框的方法,该对话框始终显示对话框,但仅调用Android M及更高版本上的权限API:

public void showDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Contacts access needed");
    builder.setPositiveButton(android.R.string.ok, null);
    builder.setMessage("please confirm Contacts access");
    builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialog) {
            // Only call the permission request api on Android M
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, PERMISSION_REQUEST_CONTACT);
            }
        }
    });
    builder.show();
}

Then on Android M and above, only call it when needed, below M, always call it:

然后在Android M及以上,只在需要时调用它,在M下面,总是调用它:

public void askForContactPermission(){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ContextCompat.checkSelfPermission(this,Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_CONTACTS)) {
                showDialog();
            }
        }
    } else {
        showDialog();
    }
}

#1


1  

Create a method for showing a permission request dialog, that always shows the dialog, but only calls the permission API on Android M and above:

创建一个显示权限请求对话框的方法,该对话框始终显示对话框,但仅调用Android M及更高版本上的权限API:

public void showDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Contacts access needed");
    builder.setPositiveButton(android.R.string.ok, null);
    builder.setMessage("please confirm Contacts access");
    builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialog) {
            // Only call the permission request api on Android M
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, PERMISSION_REQUEST_CONTACT);
            }
        }
    });
    builder.show();
}

Then on Android M and above, only call it when needed, below M, always call it:

然后在Android M及以上,只在需要时调用它,在M下面,总是调用它:

public void askForContactPermission(){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ContextCompat.checkSelfPermission(this,Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_CONTACTS)) {
                showDialog();
            }
        }
    } else {
        showDialog();
    }
}