5

से इनकार किया है संपर्क के नाम को पढ़ने के लिए मैं कैसे नए अनुमति मॉडल एप्लिकेशन सेटिंग में इतना काम करता है मैं Contacts को निष्क्रिय जाँच करना चाहते हैं अनुमति देता है READ_CONTACTS। तब मैं एप्लिकेशन और Contacts पढ़ने की कोशिश करने के लिए जा सकते हैं और ... यह थोड़े काम करता है:एंड्रॉयड 6.0 (Marshmallow) अनुमति जब अनुमति

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 कोशिश

java.lang.SecurityException: अनुमति इनकार: पढ़ने com.android.providers.contacts.HtcContactsProvider2 uri सामग्री: पीआईडी ​​= 20123, यूआईडी से //com.android.contacts/data/phones = 10,593 Android आवश्यक है। अनुमति। READ_CONTACTS, या grantUriPermission()

वह क्यों है?

संबंधित सामान: Contact data leakage via pick activities

+1

ध्यान दें कि Google से ContactsContract उपयोग नहीं कर रहे, जैसा कि आप देख सकते हैं, हम देखते हैं कि आप com.android.providers.contacts.HtcContractsProvider, हो सकता है एचटीसी तक पहुँच रहे हैं सत्यापन अनुमति के साथ एक मुद्दा है। मेरा सुझाव एक एओएसपी डिवाइस की जांच करना होगा और देखें कि क्या वही बात सच है। यदि यह सच नहीं है, तो इसका मतलब है कि यह एचटीसी के अंदर एक अनुमति रिसाव मुद्दा है। यदि वे वही हैं, तो यह एओएसपी मुद्दे की तरह लगता है। – JoxTraex

+0

यह नेक्सस पर भी पुन: उत्पन्न होता है। –

उत्तर

14

एंड्रॉयड मार्शमैलो नई अनुमतियों की प्रणाली है। आपको रन टाइम पर अनुमतियों का अनुरोध करने की आवश्यकता है। 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 
    } 
} 
+0

कृपया मेरे प्रश्न को अधिक सावधानी से पढ़ें। –

+0

ओह क्षमा करें मैंने सोचा कि आप एक समाधान चाहते हैं। –

+0

वहाँ कुछ कोड जोड़े जाने के लिए के बाद '// एक expanation ... दिखाएँ' टिप्पणी माना जाता है? –

1
@Override 
    public void onClick(View v) { 
     switch (v.getId()){ 
      case R.id.button:{ 
       askForContactPermission(); 
       break; 
      } 
     } 
    } 

    private void getContact(){ 
     Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null); 
     while (phones.moveToNext()) 
     { 
      String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
      String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
      Toast.makeText(getApplicationContext(),name+" "+phoneNumber, Toast.LENGTH_LONG).show(); 

     } 
     phones.close(); 
    } 

    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(); 
     } 
    } 

    @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 { 
        Toast.makeText(this, "No Permissions ", Toast.LENGTH_SHORT).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 
     } 
    } 
+0

हमेशा के लिए "Toast.makeText (यह," नहीं अनुमतियां जा रहा ", Toast.LENGTH_SHORT) .show();" और अनुमति पॉपअप पूछो मत –

संबंधित मुद्दे