ContentProvider提供了不同应用程序之间数据传递的接口,实现了不同应用程序之间的数据传递。我常见的ContentProvider比如通讯录,多媒体文件等等。这些是系统提供的ContentProvider。其实我们一般都是使用系统提供的ContenProvider,很少自己实现ContentProvider,不过自己实现ContenProvider会对系统提供的ContentProvider理解更透彻一些,下面先实现一个ContentProvider,然后来访问。
ContentProvider使用的数据不受限制,可以是文件,SharedPrefrences,数据库等等,这里我以SOQLite为例说明,因为SQLite更为简单。使用SQLite一定要有一列为_id.
下面是SQLite的实现
package com.zhycheng.contentprovider;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class MyDatabaseHelper extends SQLiteOpenHelper{
public MyDatabaseHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table user(_id integer primary key autoincrement,name text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
我创建了一张user表,包含_id和name。
下面是ContentProvider的实现
package com.zhycheng.contentprovider;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
public class MyContentProvier extends ContentProvider{
MyDatabaseHelper dh;
@Override
public boolean onCreate() {
// TODO Auto-generated method stub
dh=new MyDatabaseHelper(getContext(),"zhycheng",null,1);
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteDatabase db=dh.getReadableDatabase();
Cursor c=db.query("user", projection, selection, selectionArgs, null, null, sortOrder);
return c;
}
@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO Auto-generated method stub
SQLiteDatabase db=dh.getWritableDatabase();
db.insert("user", null, values);
return uri;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;
}
}
我实现了ContentProvider的插入和查询方法。下面是注册ContentProvider
<provider android:name="MyContentProvier"
android:authorities="zhycheng.liuyali"
访问ContentProvider的话如下
package com.zhycheng.contentprovider;
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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ContentProviderActivity extends Activity implements OnClickListener {
Button b1,b2,b3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1=(Button) findViewById(R.id.button1);
b2=(Button) findViewById(R.id.button2);
b3=(Button) findViewById(R.id.button3);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.button1:
MyDatabaseHelper mh=new MyDatabaseHelper(this,"zhycheng",null,1);
mh.getReadableDatabase();
break;
case R.id.button2:
ContentResolver cr=this.getContentResolver();
ContentValues cv=new ContentValues();
cv.put("name", "qwert");
cr.insert(Uri.parse("content://zhycheng.liuyali"), cv);
break;
case R.id.button3:
ContentResolver crr=this.getContentResolver();
Cursor c=crr.query(Uri.parse("content://zhycheng.liuyali"), new String[]{"_id","name"}, null, null, null);
while(c.moveToNext())
{
System.out.println(c.getString(1));
}
break;
}
}
}
注意:Uri.parser("content://zhycheng.liuyali");中的后面"zhycheng.liuyali"一定要和注册ContentProvider中的authorities的值一样才能访问ContentProvider。