本章节翻译自《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"/>
分享到:
相关推荐
android-关于ContentProvider的使用例子 有详细注释
Android知识点ContentProvider篇
Android应用开发使用ContentProvider以及SQLite实现对数据库的相关操作,可以直接操作数据库,也可以使用ContentProvider来对数据库进行间接操作。
1、Android Room操作SQLite数据 2、ContentProvider使用,使用Room进行数据库操作
从头学Android之ContentProvider示例源代码
NULL 博文链接:https://sunzone.iteye.com/blog/1884167
android contentprovider使用示例
android 高焕堂讲解 ContentProvider 范例
《Android应用程序开发教程(第2版)》教学课件01Android系统与开发环境.pdf《Android应用程序开发教程(第2版)》教学课件01Android系统与开发环境.pdf《Android应用程序开发教程(第2版)》教学课件01Android系统与开发...
android开发contentprovider教学ppt(内部资料).pptx
Android高级编程雪梨作业之自定义ContentProvider 将任务01生词本作业中生成的生词本数据库通过自定义ContentProvider的方式,共享给其他应用。 要求如下: (1) 使用自定义SQLiteOpenHelper来管理数据库; (2) 提交...
主要介绍了Android开发教程之ContentProvider数据存储的相关资料,需要的朋友可以参考下
前言 Content Provider为存储数据和获取...使用ContentProvider的好处是开发人员不需要考虑数据内部是怎么存储的,比如说如果我们想利用ContenProvider来存数据,只需告诉insert函数该ContentProvider的uri和想存
Android中ContentProvider的示例,详细可以参见博客:http://www.cnblogs.com/plokmju/p/android_ContentProvider.html
android 自定义 ContentProvider 以及 ContentResolver
此为ContentResolver与ContentProvider的使用Demo.
Android ContentProvider简单实现
的 Android SDK 提供了在 Android 平台上使用 JaVa 语言进行 Android 应用开发必须的工具和 API 接口。 特性 • 应用程序框架 支持组件的重用与替换 • Dalvik Dalvik Dalvik Dalvik 虚拟机 专为移动设备优化 • ...
同我前面的资源一样,我的每个功能的实例都是力图实现最简短话...这个实例不讲求大而全的实现ContentProvider的所有功能,而只是为了便于理解而实现。对应于我的博客“How:ContentProvider基本功能核心框架(增删改查)”