使用内容提供者ContentProvider与内容访问者ContentResolver实现拿到另外一个项目的数据库数据
来源:互联网 发布:手机淘宝哪儿有一元抢 编辑:程序博客网 时间:2024/05/16 10:40
比如说你要拿手机电话本的电话号码,或者相册,等等存到数据库的东西,那么内容提供者ContentProvider与内容访问者ContentResolver便是最好的工具
下面就给大家上代码进行操作
第一步:
写一个Module,新建一个MyProvider.java文件,重写查询的方法,当然也可以去用增删改,这里我就只演示查了
public class MyProvider extends ContentProvider{ @Override public boolean onCreate() { return false; } @Nullable @Override public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) { //获得数据库 DbHelper dbHelper=new DbHelper(getContext(),"G160628.db",null,3); //调用写的database //实际开发读写都可以用 SQLiteDatabase sqLiteDatabase = dbHelper.getWritableDatabase(); //进行查询 Cursor cursor=sqLiteDatabase.rawQuery("select * from login",null); return cursor; } @Nullable @Override public String getType(@NonNull Uri uri) { return null; } @Nullable @Override public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) { return null; } @Override public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) { return 0; } @Override public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) { return 0; }}第二步:配置,加入以下配置信息
<!--配置 1、authorities必选写上项目的package名+命名 2、name 写的是你的内容提供者 --> <provider android:authorities="com.example.g160628_android16_sqlite.ZhongZi" android:name=".MyProvider" android:exported="true"></provider> </application>
第四步:接收内容,在你自己要获得该数据的地方写内容访问者
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //getContentResolver获得内容提供者 ContentResolver contentResolver=getContentResolver(); //转Uri,解析类 记得在authorities前面加content://协议 Uri uri=Uri.parse("content://com.example.g160628_android16_sqlite.ZhongZi"); //接收ContentResolver的游标 Cursor cursor=contentResolver.query(uri,null,null,null,null); //循环打印值 while (cursor.moveToNext()){ String uname=cursor.getString(cursor.getColumnIndex("uname")); Log.i("test",uname); } }}
小编就整理到这啦,有什么疑问的可以私信我
阅读全文
1 0
- 使用内容提供者ContentProvider与内容访问者ContentResolver实现拿到另外一个项目的数据库数据
- ContentProvider内容提供者与ContentResolver内容访问者
- ContentProvider内容提供者与ContentResolver内容访问者
- ContentProvider内容提供者与ContentResolver内容访问者
- Android之ContentProvider(内容的提供者)与ContentResolver(内容访问者)
- ContentProvider(内容提供者)与ContentResolver(内容访问者)
- ContentProvider内容提供者与ContentResolver内容访问者(二)
- 25.ContentProvider自定义内容提供者与ContentResolver内容访问者
- ContentProvider内容提供者 和 ContentResolver内容访问者
- Android----ContentProvider(内容提供者) ContentResolver(内容访问者)
- ContentProvider内容提供者和ContentResolver内容访问者
- ContentProvider内容提供者和ContentResolver内容访问者
- ContentProvider内容提供者和ContentResolver内容访问者
- 内容提供者ContentProvider与内容接受者ContentResolver的使用详解
- 给大家简述下ContentProvider(内容提供者)与ContentResolver(内容访问者)如何使用。
- android的与ContentResolver(内容访问者)对ContentProvider(内容提供者)的增删查改
- Android ContentProvider(内容提供者{模拟底层})和Contentresolver(内容接受者{实现对ContentProvider数据的修改})
- Android 中ContentProvider内容提供者和ContentResolver内容访问者
- 第四次作业 LBG矢量量化算法
- POJ2376 Cleaning Shifts (贪心)
- 用位运算实现加法
- Zookeeper、hadoop集群搭建详细过程
- 面向对象——object
- 使用内容提供者ContentProvider与内容访问者ContentResolver实现拿到另外一个项目的数据库数据
- (73)Map公共方法演示、Map集合获得键值对的两种方法:entrySet和keySet。Map中元素唯一性练习
- Git:冲突--error: Your local changes to the following files would be overwritten
- Intellij idea 2017后台任务
- [Leetcode] 3. Longest Substring Without Repeating Characters
- 面向对象——多态
- vi常用技巧和命令
- 解决eclipse里不能创建tomcat7.0的server
- React-native学习篇之一(开发环境准备)