ContentProvider定义
ContentProvider是Android四大组件之一
ContentProvider是一种数据包装器(发布者)
ContentProvider主要用于不同应用程序间,不同进程间的数据共享
ContentResolver定义
ContentResolver是数据调用者,ContentResolver将数据发布出来后,通过ContentResolver对象结合Uri进行调用
通过ContentResolver可以调用ContentProvider的增、删、改、查操作
Uri定义
通用资源标识符(Universal Resource Identifier)简称Uri
Uri代表要操作数据的地址,每个ContentProvider发布数据时都有一个唯一的地址
什么是Uri
创建自定义ContentProvider的步骤
1.使用SQLite技术,创建好数据库和数据表
2.新建类继承ContentProvider
3.创建UriMatcher,定义Uri规则
4.重写6个抽象方法:(onCreate 、query、getType、insert、delete、update)
5.在AndroidManifest中注册provider
6.ContentResolver对ContentProvider共享的数据进行增删改查操作
ContentProvider实例应用(两个app间进行数据的增删改查)
在Manifest中注册provider
<provider
android:name=".provider.DatabaseProvider"
android:authorities="com.example.administrator.databasetest.provider"
android:enabled="true"
android:exported="true"></provider>
新建类继承ContentProvider,创建UriMatcher,重写6个抽象方法,定义Uri规则
//新建类继承ContentProvider
public class StudentProvider extends ContentProvider {
private MyDBHelpter myDBHelpter;
private static final String TAG = "StudentProvider";
//添加整形常量
public static final int USER_DIR = 0;
public static final int USER_ITEM = 1;
//创建authority
public static final String AUTHORITY = "com.example.administrator.studentdemo";
//创建UriMatcher对象
private static UriMatcher uriMatcher;
//创建静态代码块
static {
//实例化UriMatcher对象
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
//可以实现匹配URI的功能
//参数1:authority 参数2:路径 参数3:自定义代码
uriMatcher.addURI(AUTHORITY, "userdemo", USER_DIR);
uriMatcher.addURI(AUTHORITY, "userdemo/#", USER_ITEM);
}
public StudentProvider() {
Log.e(TAG, "StudentProvider: ");
}
//onCreate()方法
@Override
public boolean onCreate() {
//实现创建MyDBHelpter对象
myDBHelpter = new MyDBHelpter(getContext(), "userdbdemo", null, 1);
return true;
}
//删除数据表数据
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
//创建SQLiteDatabase对象
SQLiteDatabase db = myDBHelpter.getWritableDatabase();
int deleteInt = 0;
//匹配uri
switch (uriMatcher.match(uri)) {
case USER_DIR:
//参数1:表名 参数2:约束删除列的名字 参数3:具体行的值
deleteInt = db.delete("userdemo", selection, selectionArgs);
break;
case USER_ITEM:
String deleteId = uri.getPathSegments().get(1);
deleteInt = db.delete("userdemo", "id=?", new String[]{deleteId});
break;
default:
}
return deleteInt;
}
//插入数据
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db = myDBHelpter.getWritableDatabase();
switch (uriMatcher.match(uri)) {
case USER_DIR:
case USER_ITEM:
//参数1:表名 参数2:没有赋值的设为空 参数3:插入值
long newUserId = db.insert("userdemo", null, values);
break;
default:
break;
}
return null;
}
//查询数据
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = myDBHelpter.getWritableDatabase();
Cursor cursor = null;
switch (uriMatcher.match(uri)) {
case USER_DIR:
//参数1:表名 其他参数可借鉴上面的介绍
cursor = db.query("userdemo", projection, selection, selectionArgs, null, null, sortOrder);
break;
case USER_ITEM:
String queryId = uri.getPathSegments().get(1);
cursor = db.query("userdemo", projection, "id=?", new String[]{queryId}, null, null, sortOrder);
break;
default:
}
return cursor;
}
//更新数据
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
SQLiteDatabase db = myDBHelpter.getWritableDatabase();
int updateRow = 0;
switch (uriMatcher.match(uri)) {
case USER_DIR:
updateRow = db.update("userdemo",values,selection,selectionArgs);
break;
case USER_ITEM:
String updateId = uri.getPathSegments().get(1);
updateRow = db.update("userdemo",values,"id=?",new String[]{updateId});
break;
default:
}
return updateRow;
}
@Override
public String getType(Uri uri) {
throw new UnsupportedOperationException("Not yet implemented");
}
}
在另一个APP中启动
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button insertBtn;
private EditText editText;
private Button deleteBtn;
private Button queryBtn;
private Button updateBtn;
private String newId;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindID();
}
private void bindID() {
insertBtn = findViewById(R.id.main_insert_btn);
editText = findViewById(R.id.main_et);
deleteBtn = findViewById(R.id.main_delete_btn);
queryBtn = findViewById(R.id.main_query_btn);
updateBtn = findViewById(R.id.main_update_btn);
insertBtn.setOnClickListener(this);
deleteBtn.setOnClickListener(this);
queryBtn.setOnClickListener(this);
updateBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.main_insert_btn:
String name = editText.getText().toString();
//创建期待匹配的uri
Uri uri1 = Uri.parse("com.example.administrator.studentdemo/userdemo");
ContentValues values = new ContentValues();
values.put("name",name);
//获得ContentResolver对象,调用方法
getContentResolver().insert(uri1,values);
break;
case R.id.main_delete_btn:
String name1 = editText.getText().toString();
Uri uri2 = Uri.parse("com.example.administrator.studentdemo/userdemo");
getContentResolver().delete(uri2,"name=?",new String[]{name1});
break;
case R.id.main_query_btn:
Uri uri = Uri.parse("com.example.administrator.studentdemo/userdemo");
Cursor cursor = getContentResolver().query(uri,null,null,null,null);
cursor.moveToFirst();
do{
String name2 = cursor.getString(cursor.getColumnIndex("name"));
Log.e(TAG, "onClick: "+name2 );
}while(cursor.moveToNext());
cursor.close();
break;
case R.id.main_update_btn:
String updateStr = editText.getText().toString();
Uri uri3 =Uri.parse("com.example.administrator.studentdemo/userdemo");
ContentValues values1 = new ContentValues();
values1.put("name",updateStr);
getContentResolver().update(uri3,values1,"name=?",new String[]{""});
break;
default:
}
}
}