使用ContentProvider和ContentResolver实现应用程序间的数据共享

MyProvider项目中的MyProvider.java

package practise.lxm.myprodiver;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;

/**
 * Created by Administrator on 2015/4/9.
 * 提供Provider给其它程序调用
 */
public class MyProvider extends ContentProvider {
    @Override
    public boolean onCreate() {
        System.out.println("onCreate");
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        System.out.println("查询uri:" + uri.toString());
        return null;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        System.out.println("插入uri:" + uri.toString() + ";contentValue:" + values.getAsString("value"));
        return Uri.parse("content://testInsert");
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        System.out.println("删除uri:" + uri.toString() + ";删除条件" + selection );
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        System.out.println("更新uri:" + uri.toString() + ";contentValue:" + values.getAsString("value"));
        return 0;
    }
}
MyProvider项目中的AndroidManifest.xml中添加

 

MyResolver中的MainActivity.java

package practise.lxm.hello;

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.widget.Toast;

/**
 * 使用ContentResolver间接调用ContentProvider
 */
public class MainActivity extends Activity{

    ContentResolver contentResolver;
    Uri uri;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        contentResolver = getContentResolver();
        uri = Uri.parse("content://lxm.practise.provider");
    }

    //新增
    public void insert(View v){
        ContentValues contentValues = new ContentValues();
        contentValues.put("value","insertValue");
        Uri insertUri = contentResolver.insert(uri,contentValues);
        Toast.makeText(MainActivity.this,"新加记录的Uri为:" + insertUri.toString(),Toast.LENGTH_LONG).show();
    }

    //删除
    public void delete(View v){
        int delCount = contentResolver.delete(uri,"delete where",null);
        Toast.makeText(MainActivity.this,"删除数量为:" + delCount ,Toast.LENGTH_LONG).show();
    }

    //修改
    public void update(View v){
        ContentValues contentValues = new ContentValues();
        contentValues.put("value","updateValue");
        int updateCount = contentResolver.update(uri,contentValues,"update where",null);
        Toast.makeText(MainActivity.this,"更新数量为:" + updateCount, Toast.LENGTH_LONG).show();
    }

    //查询
    public void query(View v){
        Cursor cursor = contentResolver.query(uri,null,"select where",null,null);
        Toast.makeText(MainActivity.this,"查询结果为:" + cursor,Toast.LENGTH_LONG).show();
    }

}



你可能感兴趣的:(android数据共享)