创建应用ContentWriter和CotentReader
在应用ContentWriter创建MyProvider继承ContentProvider
public class MyProvider extends ContentProvider {
public static final Uri URI = Uri.parse("content://com.example.contentwriter");
private static final String TABLE = "t_person";
private SQLiteDatabase database;
@Override
public boolean onCreate() {
// TODO Auto-generated method stub
database = getContext().openOrCreateDatabase("db_person.db", Context.MODE_PRIVATE, null);
String sql = "create table if not exists t_person(" +
"_id integer primary key autoincrement," +
"name text," +
"sex text" +
")";
database.execSQL(sql);
return true;
}
@SuppressLint("ShowToast") @Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// TODO Auto-generated method stub
Cursor cursor = database.query(TABLE, null, selection, selectionArgs, null, null, sortOrder);
return cursor;
}
@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
database.insert(TABLE, null, values);
return null;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// TODO Auto-generated method stub
int num = database.delete(TABLE, selection, selectionArgs);
return num;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO Auto-generated method stub
int num = database.update(TABLE, values, selection, selectionArgs);
return num;
}
}
ContentWriter配置AndroidMainfest.xml,添加Provider,设置Provider属性Name为上面创建的类的名字MyProvider,Authorities为com.example.contentwriter,Exported为true
ContentWriter的MainActicity类的实现
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button_insert).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ContentValues contentValues = new ContentValues();
contentValues.put("name", "zmx");
contentValues.put("sex", "女");
getContentResolver().insert(MyProvider.URI, contentValues);
}
});
findViewById(R.id.button_update).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ContentValues contentValues = new ContentValues();
contentValues.put("name", "lhl");
getContentResolver().update(MyProvider.URI, contentValues, "name=?", new String[] {"zmx"});
}
});
findViewById(R.id.button_delete).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
getContentResolver().delete(MyProvider.URI, "name=?", new String[] {"lhl"});
}
});
}
}
ContentReader的MainActivity类的实现
public class MainActivity extends ActionBarActivity {
private static final Uri URI = Uri.parse("content://com.example.contentwriter");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Cursor cursor = getContentResolver().query(URI, null, "sex=?", new String[] {"女"}, null);
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
String name = cursor.getString(cursor.getColumnIndex("name"));
String sex = cursor.getString(cursor.getColumnIndex("sex"));
Toast.makeText(getApplicationContext(), name + sex, Toast.LENGTH_SHORT).show();
cursor.moveToNext();
}
}
}