`
king_tt
  • 浏览: 2112270 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

【Android 开发教程】ContentProvider的使用范例

 
阅读更多

本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。

原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/


想要理解ContentProvider的最佳方式就是自己动手去尝试一下。下面介绍如何使用一个内置的Contacts ContentProvider。

1. 创建一个工程,Provider。


2. main.xml中的代码。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:stackFromBottom="false"
        android:transcriptMode="normal" />

    <TextView
        android:id="@+id/contactName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/contactID"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

3. ProviderActivity.java中的代码。

public class ProviderActivity extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Uri allContacts = Uri.parse("content://contacts/people");
        Uri allContacts = ContactsContract.Contacts.CONTENT_URI;

        Cursor c;
        if (android.os.Build.VERSION.SDK_INT < 11) {
            // before Honeycomb
            c = managedQuery(allContacts, null, null, null, null);
        } else {
            // Honeycomb and later
            CursorLoader cursorLoader = new CursorLoader(this, allContacts,
                    null, null, null, null);
            c = cursorLoader.loadInBackground();
        }

        String[] columns = new String[] {
                ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.Contacts._ID };

        int[] views = new int[] { R.id.contactName, R.id.contactID };

        SimpleCursorAdapter adapter;

        if (android.os.Build.VERSION.SDK_INT < 11) {
            // before Honeycomb
            adapter = new SimpleCursorAdapter(this, R.layout.main, c, columns,
                    views);
        } else {
            // Honeycomb and later
            adapter = new SimpleCursorAdapter(this, R.layout.main, c, columns,
                    views, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        }
        this.setListAdapter(adapter);

    }

}

4. AndroidManifest.xml文件中的代码,添加权限。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.manoel.Provider"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".ProviderActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

5. 在模拟器的电脑本里面添加一些联系人。


6. 调试刚刚写好的例子。


在这个例子中,我们获取到所有保存在Contacts(电话本)这个程序里面的联系人信息,并把它们显示在ListView中。

首先,指定Contacts的URI。

        // Uri allContacts = Uri.parse("content://contacts/people");
        Uri allContacts = ContactsContract.Contacts.CONTENT_URI;
然后,检查当前设备的版本。

        Cursor c;
        if (android.os.Build.VERSION.SDK_INT < 11) {
            // before Honeycomb
            c = managedQuery(allContacts, null, null, null, null);
        } else {
            // Honeycomb and later
            CursorLoader cursorLoader = new CursorLoader(this, allContacts,
                    null, null, null, null);
            c = cursorLoader.loadInBackground();
        }
判断版本号,采用不同的方式去获取Cursor。当然了,总是用Activity的managedQuery()方法去获取Cursor也是没问题的。

最后,通过创建SimpleCursorAdapter,用ListView把联系人列表显示出来。

别忘了,还要在配置文件里面添加权限。

<uses-permission android:name="android.permission.READ_CONTACTS"/>


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics