首页 > 系统 > Android > 正文

Android 中ContentProvider内容提供者和ContentResolver内容访问者

2019-11-11 05:20:19
字体:
来源:转载
供稿:网友
	今天利用空闲时间研究了下ContentPRovider,当应用继承ContentProvider类,并重写该类用于提供数据和存储数据的方法,就可以向其他应用共享其数据。
 
@Overridepublic boolean onCreate() {    DbHelper dbHelper=new DbHelper(getContext(),"person.db",null,2);    sqLiteDatabase = dbHelper.getReadableDatabase();    //匹配器    uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);    uriMatcher.addURI("com.example.g0825_android_16.PERSONS","persons",1);    uriMatcher.addURI("com.example.g0825_android_16.PERSONS","persons/#",2);    return false;}@Nullable@Overridepublic Cursor query(Uri uri, String[] strings, String s, String[] strings1, String s1) {    int type=uriMatcher.match(uri);    switch (type){        //所有        case 1:            return sqLiteDatabase.query(false,"person",strings,s,strings1,null,null,s1,null);        case 2:            long id=ContentUris.parseId(uri);            return sqLiteDatabase.query(false,"person",strings,"_id=?",new String[]{id+""},null,null,s1,null);    }        return null;}@Nullable@Overridepublic String getType(Uri uri) {    return null;}@Nullable@Override

 

public Uri insert(Uri uri, ContentValues contentValues) { Log.i("test","insert"); contentValues.get("name"); contentValues.get("age"); sqLiteDatabase.insert("person","name",contentValues); return null;}@Overridepublic int delete(Uri uri, String s, String[] strings) { return sqLiteDatabase.delete("person",s,strings);}@Overridepublic int update(Uri uri, ContentValues contentValues, String s, String[] strings) { return sqLiteDatabase.update("person",contentValues,s,strings);}
 
2.在AndroidManifest.xml中配置
<provider    android:authorities="com.example.g0825_android_16.PERSONS"    android:name="com.zking.provider.MypersonProvider"    android:exported="true"    ></provider>
 
3.接收者中的代码
 
public void onClick(View view) {    //判断输入框是否为空    if(TextUtils.isEmpty(et_main_id.getText().toString())){        //查询所有        //协议   http://www.baidu.com        //content://        uri = Uri.parse("content://com.example.g0825_android_16.PERSONS/persons");    }else{        //查询单个        String id=et_main_id.getText().toString();        uri = Uri.parse("content://com.example.g0825_android_16.PERSONS/persons/"+id);    }    //访问数据    Cursor cursor=cr.query(uri,null,null,null,null);    while(cursor.moveToNext()){        int pid=cursor.getInt(cursor.getColumnIndex("_id"));        String name=cursor.getString(cursor.getColumnIndex("name"));        int age=cursor.getInt(cursor.getColumnIndex("age"));        Toast.makeText(MainActivity.this, pid+" "+name+" "+age, Toast.LENGTH_SHORT).show();    }}
 
 
//解析布局文件View v=getLayoutInflater().inflate(R.layout.dialog_listview,null);et_dialog_listview_name = (EditText) v.findViewById(R.id.et_dialog_listview_name);et_dialog_listview_age = (EditText) v.findViewById(R.id.et_dialog_listview_age);((Button)v.findViewById(R.id.btn_dialog_listview_sure)).setOnClickListener(new View.OnClickListener() {    @Override    public void onClick(View view) {        String name=et_dialog_listview_name.getText().toString();        String age=et_dialog_listview_age.getText().toString();        ContentValues contentValues=new ContentValues();        contentValues.put("name",name);        contentValues.put("age",age);        uri = Uri.parse("content://com.example.g0825_android_16.PERSONS");        cr.insert(uri,contentValues);        Toast.makeText(MainActivity.this, "执行添加操作", Toast.LENGTH_SHORT).show();        dialog.dismiss();//关闭当前的对话框    }
 
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表