ContentProvider和ContentResovler

1.在同一系统中装载了一个又一个的App,每个App之间有可能需要数据共享,App之间实现数据共享当然可以使用SharedPreferences、文件或数据库等,但是这种方式不仅比较麻烦,而且存在严重的安全漏洞,所以在Android4.2以后不再推荐使用这种方式,而是推荐使用ContentProvider

2.其实ContentProvider和ContentResovler是成对使用,从字面上ContentProvider和ContentResovler可翻译为:内容提供者 和 内容接收者

3.开发ContentProvider子类AlitanContentProvider

步骤1:子类需要实现父类方法query()、inser()、update()和delete()方法;

步骤2:在AndroidManifest中注册ContentProvider并指定android:authorities属性

4.AlitanContentProvider子类代码实现如下

/**

* Created by alitan2014 on 2017/9/8.

*/

public classAlitanContentProviderextendsContentProvider {

privateHandlermHandler;

public voidonMessage(Handler handler){

mHandler= handler;

}

@Override

public booleanonCreate() {

JSONObject object =newJSONObject();

try{

object.put("method","onCreate");

}catch(JSONException e) {

e.printStackTrace();

}

Message message =newMessage();

message.obj= object;

message.what=0x123;

if(mHandler!=null){

mHandler.sendMessage(message);

}

System.out.println("onCreate");

return true;

}

@Nullable

@Override

publicCursorquery(@NonNullUri uri,@NullableString[] projection,@NullableString selection,@NullableString[] selectionArgs,@NullableString sortOrder) {

JSONObject object =newJSONObject();

try{

object.put("method","query");

Message message =newMessage();

message.obj= object;

message.what=0x123;

if(mHandler!=null){

mHandler.sendMessage(message);

}

System.out.println("onCreate");

}catch(JSONException e) {

e.printStackTrace();

}

return null;

}

@Nullable

@Override

publicStringgetType(@NonNullUri uri) {

return null;

}

@Nullable

@Override

publicUriinsert(@NonNullUri uri,@NullableContentValues values) {

JSONObject object =newJSONObject();

try{

object.put("method","insert");

Message message =newMessage();

message.obj= object;

message.what=0x123;

if(mHandler!=null){

mHandler.sendMessage(message);

}

System.out.println("insert");

}catch(JSONException e) {

e.printStackTrace();

}

returnUri.parse("www.baidu.com");

}

@Override

public intdelete(@NonNullUri uri,@NullableString selection,@NullableString[] selectionArgs) {

JSONObject object =newJSONObject();

try{

object.put("method","delete");

Message message =newMessage();

message.obj= object;

message.what=0x123;

if(mHandler!=null){

mHandler.sendMessage(message);

}

System.out.println("delete");

}catch(JSONException e) {

e.printStackTrace();

}

return0;

}

@Override

public intupdate(@NonNullUri uri,@NullableContentValues values,@NullableString selection,@NullableString[] selectionArgs) {

JSONObject object =newJSONObject();

try{

object.put("method","update");

Message message =newMessage();

message.obj= object;

message.what=0x123;

if(mHandler!=null){

mHandler.sendMessage(message);

}

System.out.println("update");

}catch(JSONException e) {

e.printStackTrace();

}

return0;

}

}

5.在Androidmanifest中注册AlitanContentProvoder如下

android:authorities="alitan2014.com.contentprovider.AlitanContentProvider"

android:name=".AlitanContentProvider"

android:exported="true"

/>

6.在另外一个应用获取ContentResovler,并调用query(),insert(),update,delete()方法,实际上是通过ContentProvider暴露在外部的URI,调用到了AlitanContentProvider中的query(),insert(),update,delete()方法,实现方法如下

ublic classMainActivityextendsAppCompatActivity {

privateContentResolverresolver;

privateUriuri= Uri.parse("content://alitan2014.com.contentprovider.AlitanContentProvider/");

@Override

protected voidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

resolver= getContentResolver();

}

public voidquery(View view){

Cursor cursor =resolver.query(uri,null,"query_where",null,null);

Toast.makeText(MainActivity.this,"其他应用返回的内容:"+cursor,Toast.LENGTH_SHORT).show();

}

public voidinsert(View view){

ContentValues contentValues =newContentValues();

contentValues.put("method","insert");

Uri newUri =resolver.insert(uri,contentValues);

Toast.makeText(MainActivity.this,"新插入的URI为:"+newUri,Toast.LENGTH_SHORT).show();

}

public voidupdate(View view){

ContentValues contentValues =newContentValues();

contentValues.put("method","update");

intcount =resolver.update(uri,contentValues,"update_where",null);

Toast.makeText(MainActivity.this,"更新的的URI为:"+count,Toast.LENGTH_SHORT).show();

}

public voiddelete(View view){

ContentValues contentValues =newContentValues();

contentValues.put("method","update");

intcount =resolver.delete(uri,"delete_where",null);

Toast.makeText(MainActivity.this,"delete返回值为:"+count,Toast.LENGTH_SHORT).show();

}

}

你可能感兴趣的:(ContentProvider和ContentResovler)