目 录
PersonCp
PersonCp.java
insert()
ContentObserver
音乐播放器
1、添加读写权限
1.1、动态权限授予(调用封装好的方法)
2、获取音乐文件(MainActivity.java)
2、Music.java(实体类)
申请访问SD卡的权限
设置适配器
下拉刷新
PersonCp
PersonCp.java
package cn.wangzg.personcp;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import java.util.Objects;
public class PersonCp extends ContentProvider { //数据库作为数据源,将数据保存到数据库中。
private MyHelper mHelper;
private final static String AUTHORITY = "cn.wangzg.personprovider";
private static UriMatcher mUriMatcher;
private static final int PERSON_DIR = 0;
private static final int PERSON = 1;
static {
mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
// 该URI表示返回所有的person,其中PERSONS为该特定Uri的标识码
mUriMatcher.addURI(AUTHORITY, "person", PERSON_DIR);
// 该URI表示返回某一个person,其中PERSON为该特定Uri的标识码
mUriMatcher.addURI(AUTHORITY, "person/#", PERSON);
}
@Override
public String getType(Uri uri) {
switch (mUriMatcher.match(uri)) {
case PERSON_DIR:
return "vnd.android.cursor.dir/" + AUTHORITY + ".persons";
case PERSON:
return "vnd.android.cursor.item/" + AUTHORITY + ".person";
default:
throw new IllegalArgumentException("unknown uri" + uri.toString());
}
}
@Override
public boolean onCreate() {
mHelper = new MyHelper(getContext());
return true;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db = mHelper.getWritableDatabase();
switch (mUriMatcher.match(uri)) {
case PERSON_DIR:
long newId = db.insert("person", "name,phone,salary", values);
//向外界通知该ContentProvider里的数据发生了变化 ,以便ContentObserver作出相应
getContext().getContentResolver().notifyChange(uri, null);
return ContentUris.withAppendedId(uri, newId);
default:
throw new IllegalArgumentException("unknown uri" + uri.toString());
}
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
SQLiteDatabase db = mHelper.getWritableDatabase();
int updatedNum = 0;
switch (mUriMatcher.match(uri)) {
// 更新表
case PERSON_DIR:
updatedNum = db.update("person", values, selection, selectionArgs);
break;
// 按照id更新某条数据
case PERSON:
long id = ContentUris.parseId(uri);
String where = "id=" + id;
if (selection != null && !"".equals(selection.trim())) {
where = selection + " and " + where;
}
updatedNum = db.update("person", values, where, selectionArgs);
break;
default:
throw new IllegalArgumentException("unknown uri" + uri.toString());
}
//向外界通知该ContentProvider里的数据发生了变化 ,以便ContentObserver作出相应
Objects.requireNonNull(getContext()).getContentResolver().notifyChange(uri, null);
return updatedNum;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = mHelper.getWritableDatabase();
int deletedNum = 0;
switch (mUriMatcher.match(uri)) {
// 删除表
case PERSON_DIR:
deletedNum = db.delete("person", selection, selectionArgs);
break;
// 按照id删除某条数据
case PERSON:
long id = ContentUris.parseId(uri);
String where = "id=" + id;
if (selection != null && !"".equals(selection.trim())) {
where = selection + " and " + where;
}
deletedNum = db.delete("person", where, selectionArgs);
break;
default:
throw new IllegalArgumentException("unknown uri" + uri.toString());
}
//向外界通知该ContentProvider里的数据发生了变化 ,以便ContentObserver作出相应
Objects.requireNonNull(getContext()).getContentResolver().notifyChange(uri, null);
return deletedNum;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = mHelper.getWritableDatabase();
Cursor cursor = null;
switch (mUriMatcher.match(uri)) {
// 查询表
case PERSON_DIR:
cursor = db.query("person", projection, selection, selectionArgs, null, null, sortOrder);
break;
// 按照id查询某条数据
case PERSON:
// 第一步:
long id = ContentUris.parseId(uri);
String where = "id=" + id;
// 第二步:
if (selection != null && !"".equals(selection.trim())) {
where = selection + " and " + where;
}
cursor = db.query("person", projection, where, selectionArgs, null, null, sortOrder);
break;
default:
throw new IllegalArgumentException("unknown uri" + uri.toString());
}
return cursor;
}
}
insert()
ContentObserver
菜鸟教程 【4.4.1 ContentProvider初探】
https://www.runoob.com/w3cnote/android-tutorial-contentprovider.html
音乐播放器
1、添加读写权限
1.1、动态权限授予(调用封装好的方法)
2、获取音乐文件(MainActivity.java)
2、Music.java(实体类)
申请访问SD卡的权限
设置适配器
下拉刷新
点个赞吧~ b( ̄▽ ̄)d
求求了~
小本生意,不容易啊~