ContentProvider&ContentResolver
ContentProvider&ContentResolver
ContentProvider 的介绍和使用
什么是ContentProvider?
ContentProvider内容提供者(四大组件之一)主要用于在不同的应用程序之间实现数据共享的功能,使用时需要导入注解的包并在清单文件中注册,就是简而言之内容提供者提供一个地址,分享内容内容解析者把uri解析一下,去指向提供者的地址
使用步骤
(1)创建数据库,自定义类继承SQLiteOpenHelper
(2)自定义内容提供者类,继承ContentProvider,重写insert/delete/update/query方法,向外界提供方法
(3)清单文件中注册内容提供者
自定义ContentProvider
1.提供方:ContentProvider
(1)创建数据库,自定义类继承SQLiteOpenHelper
(2)自定义内容提供者类,继承ContentProvider,重写insert/delete/update/query方法,向外界提供方法
(3)清单文件中注册内容提供者
2.索要方:ContentResolver
(1)通过Context的getContentResolver方法获得ContentResolver对象
(2)确定uri
(3)调用insert/delete/update/query方法访问数据库
ContentProvider
内容解析者
(1)通过Context的getContentResolver方法获得ContentResolver对象
(2)确定uri
(3)调用insert/delete/update/query方法访问数据库
自定义权限
写在清单文件中,application外面
<permission
android:name="com.bawei.permission.READ"
android:protectionLevel="normal"
>
</permission>
<permission
android:name="com.bawei.permission.WRITE"
android:protectionLevel="normal"
>
</permission>
内容解析方清单文件添加权限
<uses-permission android:name="com.bawei.permission.WRITE"></uses-permission>
<uses-permission android:name="com.bawei.permission.READ"></uses-permission>
基本展示案例代码
public class MyHelper extends SQLiteOpenHelper {
public MyHelper(Context context) {
super(context, "user.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table user (_id integer primary key autoincrement ,name varchar(30),age integer)");
for(int i=1;i<10;i++){
db.execSQL("insert into user values(null,?,?)",new String[]{"非常的帅"+i+"",i+""});
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
public class MyProvider extends ContentProvider {
private MyHelper myHelper;
private SQLiteDatabase db;
//获取数据库
@Override
public boolean onCreate() {
myHelper=new MyHelper(getContext());
db=myHelper.getWritableDatabase();
if(db==null){
return false;
}else{
return true;
}
}
@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
return db.query("user",projection,selection,selectionArgs,null,null,sortOrder);
}
@Nullable
@Override
public String getType(@NonNull Uri uri) {
return null;
}
@Nullable
@Override
public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
db.insert("user",null,values);
return uri;
}
@Override
public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
return db.delete("user",selection,selectionArgs);
}
@Override
public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
return db.update("user",values,selection,selectionArgs);
}
}
public class Main2Activity extends AppCompatActivity {
private ContentResolver contentResolver;
private Uri uri;
private Button buttonId;
private static final String TAG = "Main2Activity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uri=Uri.parse("content://com.bawei");
contentResolver=getContentResolver();
buttonId = findViewById(R.id.button_id);
buttonId.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
query();
}
});
//
}
//user (_id integer primary key autoincrement ,name varchar(30),age integer)
public void query(){
//参数一:uri 参数二:查询的字段 参数三:条件 参数四:为?赋值 参数五:排序
Toast.makeText(this, "cha", Toast.LENGTH_SHORT).show();
Cursor cursor=contentResolver.query(uri,new String[]{"_id","name"},null,null,null);
while (cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex("name"));
Log.i(TAG, "query: "+name);
Toast.makeText(this, name, Toast.LENGTH_SHORT).show();
}
//遍历
}
public void delete(){
//参数一:uri 参数二:条件 参数三:为?赋值
int raw=contentResolver.delete(uri,"name=?",new String[]{"非常的帅1"});
}
public void insert(){
//添加数据
ContentValues contentValues=new ContentValues();
contentValues.put("name","要甜雪");
contentValues.put("age",18);
contentResolver.insert(uri,contentValues);
//遍历
}
}
通讯录案例代码
创建外部类储存联系人信息
public class Phone {
private String name;
private String number;
public Phone() {
}
public Phone(String name, String number) {
this.name = name;
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
@Override
public String toString() {
return "Phone{" +
"name='" + name + '\'' +
", number='" + number + '\'' +
'}';
}
}
主函数获取通讯录
public class MainActivity extends AppCompatActivity {
private Button buttonId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonId = findViewById(R.id.button_id);
buttonId.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//根据版本动态获取授权(6.0以上)
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
requestPermissions(new String[]{Manifest.permission.WRITE_CONTACTS,Manifest.permission.READ_CONTACTS},100);
}else{
resolverMethod();
}
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == 100 & grantResults[0] == PackageManager.PERMISSION_GRANTED){
resolverMethod();
}
}
//因为重复使用,所有封装
private void resolverMethod() {
ContentResolver contentResolver = getContentResolver();
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] strings = {ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME};
Cursor cursor = contentResolver.query(uri, strings, null, null, null);
while (cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Toast.makeText(MainActivity.this, name, Toast.LENGTH_SHORT).show();
//有了对象,其他都好说了.
Phone phone = new Phone(name, number);
}
}
}
上一篇: 软件构造LAB1
下一篇: 《软件构造》Lab 1