欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ContentProvider内容提供者

程序员文章站 2022-07-08 10:42:27
...

ContentProvider介绍:
1.ContentProvider内容提供者(四大组件之一)主要用于在不同的应用程序之间实现数据共享的功能。
2.技能get点:
(1)访问系统自带的内容提供者(******)
手机联系人/音频/视频/图库/短信/系统设置
(2)自己写内容提供者

uri:ContactsContract.CommonDataKinds.Phone.CONTENT_URI
字段:ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME:姓名
ContactsContract.CommonDataKinds.Phone.NUMBER:电话

public void click2(View view) {
ContentResolver resolver = getContentResolver();
Uri uri= Telephony.Sms.CONTENT_URI;
String[] strs={Telephony.Sms.BODY,Telephony.Sms.ADDRESS};
Cursor cursor=resolver.query(uri,strs,null,null,null);
//第一种ListView 中 第二种 :存到数据库
ArrayList list=new ArrayList<>();
while(cursor.moveToNext()){
String address=cursor.getString(cursor.getColumnIndex(Telephony.Sms.ADDRESS));
String body=cursor.getString(cursor.getColumnIndex(Telephony.Sms.BODY));
list.add(new Msm(address,body));
//执行insert语句
}

}
class Msm{
    private String address;
    private String body;

    public Msm(String address, String body) {
        this.address = address;
        this.body = body;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }
}
public class MyHelper extends SQLiteOpenHelper {
//TODO 2: 构造
public MyHelper(Context context) {
    super(context, "student.db", null, 1);
}
//TODO 3:重写父类的方法
/**
 * 创建数据库时调用, 只执行一次  -- 初始化表, 初始化数据
 * @param db  数据库的操作类
 */
@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table student(_id integer primary key autoincrement,name varchar(20),age integer)");
}

/**
 * 更新数据 --- 如果数据库的 版本号发生变化, 执行该方法 --- 执行多次
 */
@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);
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
(3)清单文件中注册内容提供者

    <provider
        android:exported="true"
        android:authorities="com.bawei"
        android:name=".MyProvider"></provider>

public void click1(View view) {
//TODO 1:获得ContentResolver
ContentResolver resolver = getContentResolver();
//TODO 2:确定uri和要查询的字段
Uri uri= ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] strs={ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER};
//TODO 3:执行查询并将结果遍历展现在ListView中和存储到本地的数据库中
Cursor cursor=resolver.query(uri,strs,null,null,null);
ArrayList list=new ArrayList<>();
while(cursor.moveToNext()){
String name=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
list.add(new Phone(name,number));
//将每条记录存储到自己的数据库中
}
//将list集合中的数据展现在ListView中,此处省略
}
class Phone{
private String name;
private String number;

    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;
    }
}