【问题标题】:Why am I able to retrieve my contacts even without android.permission.READ_CONTACTS?为什么即使没有 android.permission.READ_CONTACTS 也能检索我的联系人?
【发布时间】:2014-03-24 17:11:31
【问题描述】:

我编写了一些代码来获取所有联系人,但忘记在 AndroidManifest.xml 中声明权限。

AndroidManifest.xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ninja.mobilehelper" >
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.ninja.mobilehelper.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
</manifest>

Java 代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Uri contactsUri= ContactsContract.Contacts.CONTENT_URI;
    String[] proj1=new String[]{ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts.HAS_PHONE_NUMBER,
            ContactsContract.Contacts.LOOKUP_KEY};
    Cursor curContacts=getContentResolver().query(contactsUri,proj1, null, null, null);

    //declare a ArrayList object to store the data that will present to the user
    ArrayList<String> contactsList=new ArrayList<String>();
    String allPhoneNo="";
    if(curContacts.getCount()>0){
        while(curContacts.moveToNext()){
            // get all the phone numbers if exist
            if(curContacts.getInt(1)>0){
                allPhoneNo=getAllPhoneNumbers(curContacts.getString(2));
            }
            contactsList.add(curContacts.getString(0)+" , "+allPhoneNo);
            allPhoneNo="";
        }
    }

    // binding the data to ListView
    setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,        contactsList));
    ListView lv=getListView();
    lv.setTextFilterEnabled(true);

}

我的手机是 Moto X,Android 4.4.2,使用 IntelliJIdea。

Gradle builds.Android build tool version is '19.0.3'.

似乎需要使用权限 android:name="android.permission.READ_CONTACTS" ,但未经许可即可使用!为什么?!

有什么建议吗?

补充:是我的错,我打开了previous应用,声明了权限。好伤心,我好像发现了Android的一个bug - -!!

【问题讨论】:

    标签: java android permissions


    【解决方案1】:

    是的,如果您在代码中授予 uri 权限,它将起作用。

     grantUriPermission(getPackageName(),ContactsContract.Contacts.CONTENT_URI, Intent.FLAG_GRANT_READ_URI_PERMISSION);
    

    【讨论】:

    • 据我了解,这段代码应该放在Provider中,以允许其他应用程序访问。我 -1 的原因是因为它表明这行代码被放入 ListView 的 onCreate 中,我认为这不是真的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-14
    • 1970-01-01
    • 2012-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-18
    相关资源
    最近更新 更多