在Content 3.1 Android 3.1中列出内容

时间:2011-10-21 14:26:54

标签: java android android-contentprovider

我在Motorola xoom(SDK Lvl 12)上使用android 3.1,我想知道如何知道哪些数据可以通过内容提供商访问。

是否有可能“列出”内容提供商,知道其内部是什么?

我尝试通过以下代码从平板电脑访问联系人:

        Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

但它每次都给我一个错误,我可能做错了,这就是为什么我想知道什么是可访问的以及如何。

编辑:平板电脑提供的错误如下:

The application xx(process.com.xx.xx) has stopped unexpectedly. Please try again.

这是我的manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.x.y"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="12" />
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".xy"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <uses-permission android:name="android.permission.READ_CONTACTS" />
        <uses-permission android:name="android.permission.ACCESS_CHECKIN_PROPERTIES" />
    </application>
</manifest>

并且调试器说

    10-21 15:45:40.789: DEBUG/dalvikvm(141): GC_EXPLICIT freed 8K, 11% free 18190K/20295K, paused 3ms+3ms
10-21 15:45:41.859: ERROR/DatabaseUtils(197): Writing exception to parcel
10-21 15:45:41.859: ERROR/DatabaseUtils(197): java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/contacts from pid=406, uid=10039 requires android.permission.READ_CONTACTS
10-21 15:45:41.859: ERROR/DatabaseUtils(197):     at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:309)
10-21 15:45:41.859: ERROR/DatabaseUtils(197):     at android.content.ContentProvider$Transport.bulkQuery(ContentProvider.java:178)
10-21 15:45:41.859: ERROR/DatabaseUtils(197):     at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:111)
10-21 15:45:41.859: ERROR/DatabaseUtils(197):     at android.os.Binder.execTransact(Binder.java:320)
10-21 15:45:41.859: ERROR/DatabaseUtils(197):     at dalvik.system.NativeStart.run(Native Method)
10-21 15:45:41.869: DEBUG/AndroidRuntime(406): Shutting down VM
10-21 15:45:41.869: WARN/dalvikvm(406): threadid=1: thread exiting with uncaught exception (group=0x40014760)
10-21 15:45:41.889: ERROR/AndroidRuntime(406): FATAL EXCEPTION: main
10-21 15:45:41.889: ERROR/AndroidRuntime(406): java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/contacts from pid=406, uid=10039 requires android.permission.READ_CONTACTS

谢谢。

1 个答案:

答案 0 :(得分:0)

尝试此操作以获取联系人:

    Cursor cur = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.Contacts.DISPLAY_NAME + " asc");
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            Log.d("TAG", "ID: " + id + " Name: " + name);
        }
    }
    cur.close();