Content Provider提供了一种多应用间数据共享的方式,比如:联系人信息可以被多个应用程序访问。Content Provider是个实现了一组用于提供其他应用程序存取数据的标准方法的类。
下面来看一个简单的实例: MainActivity.java
package com.app.contentprovidetest; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.provider.Contacts;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.util.Log;
import android.view.Menu;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast; public class MainActivity extends Activity {
//声明ContentResolver对象
private ContentResolver mContentResolver= null;
Cursor cursor ;
//声明联系人 Uri位置
private Uri uri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获得ContentResolver对象
mContentResolver=getContentResolver();
uri =ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
//cursor取数据
//(uri地址,显示字段,查询条件,条件参数,排序)
cursor = mContentResolver.query(uri, null, null, null, null);
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
if(cursor.getCount()>0){
while(cursor.moveToNext()){//游标下标移动
String name =cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME));
String phone =cursor.getString(cursor.getColumnIndex(Phone.DATA));
Log.i("xxxxxx", "name:"+name+","+"phone"+phone);
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", name);
map.put("phone", phone);
list.add(map);
}
ListView listView1 =(ListView) findViewById(R.id.listView1);
SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, list, R.layout.listview_item,
new String[]{"name","phone"}, new int[]{R.id.textView1,R.id.textView2});
listView1.setAdapter(adapter);
}
insert();//添加系统ContentProvider 内容
}
private void insert() {
//获得ContentResolver对象实例
ContentResolver cr=getContentResolver();
//将添加的信息封装到ContentValues对象
ContentValues values = new ContentValues();
//定义Uri
Uri uri = Contacts.People.CONTENT_URI;
//添加姓名
values.put(People.NAME, "Android");
//添加电话号
values.put(People.NUMBER, "12345");
//执行添加操作
cr.insert(uri, values);
Toast.makeText(getApplicationContext(), "添加成功" , Toast.LENGTH_SHORT).show();
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
R.Layout.XML文件 listview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" /> <TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" /> </LinearLayout> </LinearLayout>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1" >
</ListView> </RelativeLayout>