1.ContentProvider是什么
ContentProvider为Android四大组件之一,主要用来应用程序之间的数据共享,也就是说一个应用程序用ContentProvider将自己的数据暴露出来,
2.Content Resolver是什么
其他应用程序通过ContentResolver来对其暴露出来的数据进行增删改查
3.URI是什么,标准格式是什么
Uri是:通用资源标识符
uri标准格式:content://com.包名(三级)/资源路径path
4.UriMacher是什么
初始化urimacher
private UriMatcher uriMatcher;
addUri里面的三个参数:authority:mannifest注册的包名;path;code:匹配成功返回的匹配码
写成静态代码块的格式
{
uriMatcher=new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI("com.lenovo.studentprovider","student",1);
uriMatcher.addURI("com.lenovo.studentprovider","teacher",2);
}
5.如何使用ContentProvider
首先要有一个数据库类自己写一个数据库的类继承sqliteopenhelper,创建数据表
package com.example.xiaozhen.mytest;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by xiaozhen on 2018/6/21.
*/
public class StudentSQLite extends SQLiteOpenHelper {
private String student_sql="create table student(" +
"id integer primary key autoincrement not null," +
"name text," +
"age integer" +
")";
public StudentSQLite(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(student_sql);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
新建类继承ContentProvider,然后实现他的六个方法
初始化自己的数据库类
package com.example.xiaozhen.mytest;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.util.Log;
/**
* Created by xiaozhen on 2018/6/21.
*/
public class StudentProvider extends ContentProvider {
private String TAG="StudentProvider";
private StudentSQLite studentSqlite;
private SQLiteDatabase sqlitedb;
private UriMatcher uriMatcher;
@Override
public boolean onCreate() {
Log.e(TAG, "onCreate: ---------" );
studentSqlite=new StudentSQLite(getContext(),"student_db",null,1);
sqlitedb=studentSqlite.getWritableDatabase();
return false;
}
/* {
uriMatcher=new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI("com.lenovo.studentprovider","student",1);
uriMatcher.addURI("com.lenovo.studentprovider","teacher",2);
}*/
@Nullable
@Override
public Cursor query(Uri uri, String[] strings, String s, String[] strings1, String s1) {
Log.e(TAG, "query: --------" );
return null;
}
@Nullable
@Override
public String getType(Uri uri) {
Log.e(TAG, "getType: ----------");
return null;
}
@Nullable
@Override
public Uri insert(Uri uri, ContentValues contentValues) {
Log.e(TAG, "insert: -------" );
sqlitedb.insert("student",null,contentValues);
return null;
}
@Override
public int delete(Uri uri, String s, String[] strings) {
Log.e(TAG, "delete: --------" );
sqlitedb.delete("student",s,strings);
return 0;
}
@Override
public int update(Uri uri, ContentValues contentValues, String s, String[] strings) {
Log.e(TAG, "update: --------");
sqlitedb.update("student",contentValues,"name=?",strings);
return 0;
}
}
然后在另一个project中用contenresovler接收`
com.example.xiaozhen.webapplication;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class StuedntActivity extends AppCompatActivity implements View.OnClickListener {
private Button add_btn, delete_btn, update_btn, select_btn;
private ContentResolver contentResolver;
private Uri uri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stuednt);
init();
uri = Uri.parse("content://com.lenovo.studentprovider");//在uri后面写上表名
contentResolver = getContentResolver();
/*contentResolver.insert(uri, new ContentValues());
contentResolver.query(uri, null, null, null, null);*/
}
private void init() {
add_btn = (Button) findViewById(R.id.add_btn);
delete_btn = (Button) findViewById(R.id.delete_btn);
update_btn = (Button) findViewById(R.id.update_btn);
select_btn = (Button) findViewById(R.id.update_btn);
add_btn.setOnClickListener(this);
delete_btn.setOnClickListener(this);
update_btn.setOnClickListener(this);
select_btn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.add_btn:
ContentValues contentValues = new ContentValues();
contentValues.put("name", "Frank");
contentValues.put("age", "88");
contentResolver.insert(uri, contentValues);
break;
case R.id.delete_btn:
contentResolver.delete(uri,"name=?",new String[]{"Remove"});
break;
case R.id.update_btn:
ContentValues Values = new ContentValues();
contentResolver.update(uri,Values,"name=?",new String[] {"Frank"});
break;
case R.id.select_btn:
Cursor cursor= contentResolver.query(uri,null,null,null,null);
do{
int id=cursor.getInt(cursor.getColumnIndex("id"));
String name=cursor.getString(cursor.getColumnIndex("name"));
int age=cursor.getInt(cursor.getColumnIndex("age"));
}while (cursor.moveToNext());
break;
default:
break;
}
}
}
`