ContentProvider内容提供者
程序员文章站
2022-07-08 10:42:39
...
ContentProvider内容提供者
ContentProvider内容提供者(四大组件之一)主要用于在不同的应用程序之间实现数据共享的功能
有内容提供方和内容索要方
索要方是contentResolver
如果访问系统的自带数据需要加权限
url 是统一资源标识符
还有内容提供方 contentProvider
需要继承 ContentProlider
重写六个方法 进行创建数据库 增删改查操作
内容提供者代码
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// Implement this to handle requests to delete one or more rows.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public String getType(Uri uri) {
// TODO: Implement this to handle requests for the MIME type of the data
// at the given URI.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO: Implement this to handle requests to insert a new row.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public boolean onCreate() {
// TODO: Implement this to initialize your content provider on startup.
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// TODO: Implement this to handle query requests from clients.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO: Implement this to handle requests to update one or more rows.
throw new UnsupportedOperationException("Not yet implemented");
}
内容索要方代码
ContentResolver resolver = getContentResolver();
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] s = new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor cursor = resolver.query(uri, s, null, null, null);
while (cursor.moveToNext()) {
User user = new User();
user.name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
// user.phone = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.e("user####", user.toString());
}
ContentResolver resolver = getContentResolver();
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] strings = new String[]{MediaStore.Images.Media.TITLE, MediaStore.Images.Media.SIZE, MediaStore.Images.Media.DATA};
Cursor cursor = resolver.query(uri, strings, null, null, null);
while (cursor.moveToNext()) {
User user = new User();
user.name = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.TITLE));
// user.phone=cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.SIZE));
// user.date=cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
Log.e("pic####", user.toString());
}
推荐阅读
-
ZK中使用JS读取客户端txt文件内容问题
-
EasyExcel 轻松灵活读取Excel内容
-
Win10 Mobile预览版15043更新、修复、已知和改进内容汇总
-
Win10 PC创造者更新15042快速预览版更新内容大全
-
Win10 Build 15042更新了什么内容?Win10Build15042更新内容
-
1亿+有声内容加持!小雅Nano AI音箱图赏:软萌可爱
-
盈利模式 让用户对网站内容买单
-
Win10秋季创意者更新15213 Mobile快速预览版更新内容、已知问题汇总
-
Win10正式版1511 Build 10586.916累积更新补丁kb4019473更新内容汇总大全
-
拼多多上车直播, 猫拼狗内容电商“开拼”