所有Android版本的联系权限

问题描述:

我正在开发Android应用程序,在该应用程序中,我需要在上传联系人之前向用户询问联系*限。以下是我用于联系许可Build.VERSION.SDK_INT >= Build.VERSION_CODES.M的代码。所有Android版本的联系权限

但问题是我需要每次都要向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 
     } 
    } 
+3

在M以下,您不需要编写除Manifest文件外的任何外部代码。 – Spartan

+0

@PushpendraChoudhary感谢您的评论。其实我的要求是显示联系权限的消息。我们可以通过编程的方式要求联系*限吗? –

+0

问题在于Android M以下,它没有要求联系权限。我需要在此SDK级别下显示一个对话框。 –

用于示出许可请求对话框创建一个方法,该方法始终显示的对话框中,但只要求在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(); 
} 

然后在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(); 
    } 
} 
+0

感谢您在这里的答案。我已经放了这些代码,但它仍然没有要求许可。这不是在这种情况下,如果未来(ContextCompat.checkSelfPermission(这一点,Manifest.permission.READ_CONTACTS)!= PackageManager.PERMISSION_GRANTED){ –

+0

这意味着你已经收到此权限从用户,如果你想测试它像一个新用户,打开手机的设置应用>应用>您的应用>权限>关闭所有权限 – marmor