content provider 是android 的四大主键之一,可以实现跨进程间的通信,也就是在不同的项目中运行。通过了今天一天的学习,特地总结一下,以便以后复习之用。
在编写内容提供者是先需要继承ContentProvider类。如public class TestProvider extends ContentProvider { }。
重写public boolean onCreate(),
public Uri insert(),
public Cursor query(),
public int update(),
public int delete()方法。onCreate()方法是当第一次运行内容提供者所在的APK程序时调用的。只会调用一次。
代码如下:
<span style="white-space:pre"> </span><pre name="code" class="java">public class TestProvider extends ContentProvider {
private Sqlite sqlite;
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = this.sqlite.getReadableDatabase();
db.delete("stu", selection, selectionArgs);
return 0;
}
@Override
public String getType(Uri uri) {
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db = this.sqlite.getReadableDatabase();
db.insert("stu", null, values);
db.close();
return null;
}
/**当第一次运行内容提供者所在的APK程序*/
@Override
public boolean onCreate() {
this.sqlite = new Sqlite(this.getContext(), "database1", null, 1); //
this.sqlite.getReadableDatabase();//触发后台去创建数据库和表。
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = this.sqlite.getReadableDatabase();
Cursor cursor = db.query("stu", projection, selection, selectionArgs, null, null, sortOrder);
return cursor;
}
@Override
public int update(Uri uri, ContentValues values, String selection,String[] selectionArgs) {
SQLiteDatabase db = this.sqlite.getReadableDatabase();
db.update("stu", values, selection, selectionArgs);
db.close();
return 0;
}
}
内容提供者需要在AndroidManifest.xml中配置
<provider
android:authorities="com.wen.contextprovider.TestProvider"
android:multiprocess="true"
android:name="com.wen.contextprovider.TestProvider"
/>
android:authorities 配置很重要,是进程间通信的纽带,其它进程就是通过它找到的内容提供者。android:multiprocess="true"确定是否可以和其它程序应用组件调用或交互。android:name 是内容提供者的包名+类名。
在使用内容提供者时(另一个程序中),需要用 Uri uri = Uri.parse("content://com.wen.contextprovider.TestProvider");来找到内容提供者,content://后面跟的就是之前内容提供者配置的android:authorities 的内容。可以通过它找到内容提供者的包名和类名。
ContentResolver cr = this.getContentResolver();//创建被代理对象。
如:cr.insert(uri, values); 就是用insert()方法触发后台去调用内容提供者中的insert()方法,用的代理的设计模式。需要注意的是这 两个方法名字相同,参数相同,但确实不是同一个方法,包名都不同的。
public class MainAct extends Activity implements OnClickListener {
private EditText id, name, age;
private TextView show;
private Button insert, delete, update, query;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
this.init();
}
private void init() {
this.id =(EditText) findViewById(R.id.id);
this.name = (EditText) findViewById(R.id.name);
this.age = (EditText) findViewById(R.id.age);
this.show = (TextView) findViewById(R.id.show);
this.insert = (Button) findViewById(R.id.insert);
this.delete = (Button) findViewById(R.id.delete);
this.update = (Button) findViewById(R.id.update);
this.query = (Button) findViewById(R.id.query);
this.insert.setOnClickListener(this);
this.delete.setOnClickListener(this);
this.update.setOnClickListener(this);
this.query.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Uri uri = Uri.parse("content://com.wen.contextprovider.TestProvider");//用于找到内容提供者
ContentResolver cr = this.getContentResolver();//创建被代理对象。
String idd = this.id.getText().toString();
int id = new Integer(idd);
String name = this.name.getText().toString();
String agee = this.age.getText().toString();
int age = new Integer(agee);
switch (v.getId()) {
case R.id.insert: {
ContentValues values = new ContentValues();
values.put("id", id);
values.put("name", name);
values.put("age", age);
// 被代码对象cr
cr.insert(uri, values);
break;
}
case R.id.delete: {
String [] selectionArgs = {idd};
cr.delete(uri, "id=?", selectionArgs);
break;
}
case R.id.update: {
ContentValues values =new ContentValues();
values.put("name", name);
values.put("age", age);
String[] selectionArgs ={idd};
cr.update(uri, values, "id=?", selectionArgs);
break;
}
case R.id.query: {
String[] selectionArgs = {idd};
Cursor cursor = cr.query(uri, new String[]{"id", "name", "age"}, "id=?", selectionArgs, "name");
String inf = "";
while(cursor.moveToNext()) {
int ids = cursor.getInt(cursor.getColumnIndex("id"));
String names = cursor.getString(cursor.getColumnIndex("name"));
int ages = cursor.getInt(cursor.getColumnIndex("age"));
inf += "id="+ids+",name="+names+",age="+ages+"\n";
}
this.show.setText(inf);
}
break;
default:
break;
}
}
}
内容提供者和数据库的合作特别好,单看其中的方法的传参就可以看得出他们之间的关系匪浅。所以可以用内容提供者直接操作数据库,然后让其他的程序通过内容提供者,来间接的操作数据库。