Android ContentResolver
来源:互联网 发布:机电一体化建模软件 编辑:程序博客网 时间:2024/05/17 03:21
1.ContentResolver的基本用法
对于每一个应用程序来说,如果想要访问内容提供器中的共享数据,就一定要借助ContentResolver类,可以通过Context中的getContentResolver()方法获取到该类的实例,ContentResolver中提供了一系列的方法用于对数据进行CRUD
操作.其中insert()方法用于添加数据,update()方法用于更新数据,delete()方法用于删除数据,query()方法用于查询数据.
不同于SQLiteDatabase,ContentResolver中的增删改查方法都是不接收表名参数的,而是使用一个Uri参数代替,这个参数被称为内容URI,内容URI给内容提供器中的数据建立了唯一标识符,它主要由两部分组成:authority和path,authority是用于对不同的应用程序做区分的,一般为了避免冲突,都会采用程序包名的方式来进行命名,比如某个程序的包名是com.example.app,那么该程序对应的authority就可以命名为com.example.app.procider.path则是用于对同一个应用程序中不同的表做区分的,通常会添加到authority后面,比如某个应用的数据库里面存在两张表:table1和table2,这时就可以将path分别命名为/table1和table2,然后把authority和path进行组合,内容URI就变成了 com.example.app.procider/table1和com.example.app.procider/table2.我们还需要在字符串的头部加上协议声明,因此,内容URI的最标准的格式写法如下:
content://com.example.app.procider/table1content://com.example.app.procider/table2
在得到了内容URI字符串之后,我们还需要将它解析成Uri对象才可以作为参数传入.解析相当简单,代码如下:
Uri uri = Uri.parse("content://com.example.app.procider/table1");
只需要调用Uri.parse()方法,就可以将内容URI字符串解析成Uri对象了.
现在我们可以使用这个Uri对象来查询table1表中的数据了,代码如下:
Cursor cursor = getContentResolver().query( uri, projection, selection, selectionArgs, sortOrder);//uri from table_name 指定查询某个应用程序下的某一张表//projection select column1,column2 指定查询的列名//selection where column = value 指定where的约束条件//selectionArgs - 为where中的占位符提供具体的值//orderBy order by column1,column2 指定查询结果的排序方式
查询完成后返回的是一个Cursor对象,这时我们就可以将数据从Cursor对象中逐个读取出来,读取的思路仍然是通过移动游标的位置来遍历Cursor的所有行,然后再取出每一行中相应列的数据,代码如下:
if (cursor != null){ while(cursor.moveToNext()){ String column1 = cursor.getString(cursor.getColumnIndex("column1")); int column2 = cursor.getInt(cursor.getColumnIndex("column2")); } cursor.close();}
向table1表中添加一条数据,代码如下:
ContentValues values = new ContentValues();values.put("column1", "text");values.put("column2:, 1);getContentResolver().insert(uri, values);
将待添加的数据组装到ContentValues中,然后调用ContentResolver的insert()方法,将uri和ContentValues作为参数传入.
如果我们想要更新这条新添加的数据,把column1的值清空,可以接祖ContentResolver的update()方法实现,代码如下:
ContentValues values = new ContentValues();values.put("column1", "");getContentResolver().update(uri, values, "column1 = ? and column2 = ?", new String[] {"text", "1"});
注意上述代码使用了selection和selectionArgs参数来对想要更新的数据进行约束,以防止所有行都会受影响.
最后,可以调用ContentResolver的delete()方法将这条数据删除掉,代码如下:
getContentResolver().delete(uri, values, "column2 = ?", new String[] {"1"});
2.读取系统联系人
首先编写一下布局文件,读取出来的联系人信息显示在ListView中,修改activity_main.xml代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ListView android:id="@+id/contacts_view" android:layout_width="match_parent" android:layout_height="match_parent"/></LinearLayout>
修改MainActivity代码:
public class MainActivity extends AppCompatActivity{ ArrayAdapter<String> adapter; List<String> contactsList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView contactsView = (ListView)findViewById(R.id.contacts_view); adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, contactsList); contactsView.setAdapter(adapter); if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED){ ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS }, 1); } else { readContacts(); } } private void readContacts(){ Cursor cursor = null; try { //查询联系人数据 cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); if (cursor != null) { while (cursor.moveToNext()) { //获取联系人姓名 String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); //获取联系人手机号 String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); contactsList.add(displayName + "\n" + number); } adapter.notifyDataSetChanged(); } } catch (Exception e) { e.printStackTrace(); } finally { if (cursor != null) { cursor.close(); } } } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { switch (requestCode){ case 1: if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){ readContacts(); } else { Toast.makeText(this, "You denied the permission",Toast.LENGTH_SHORT).show(); } break; default: } }}
在onCreate()方法中,我们首先获取了ListView控件的实例,并给它设置好了适配器,然后开始调用运行时权限的处理逻辑,因为READ_CONTACTS权限是危险权限,所以要使用运行时权限处理流程,我们在用户授权之后调用readContacts()方法来读取系统联系人信息.
在readContacts()方法中,使用ContentResolver的query()方法来查询系统的联系人数据,没有调用Uri.parse()方法去解析URI字符串,这是因为ContactsContract.CommonDataKinds.Phone类已经帮我们做好了封装,提供一个CONTENT_URI常量,而这个常量就是使用Uri.parse()方法解析出来的结果,接着我们对Cursor对象进行遍历,将联系人姓名和手机号这些数据逐个取出,联系人姓名这一列对应的常量是ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,联系人手机号对应的常量是ContactsContract.CommonDataKinds.Phone.NUMBER.两个数据都取出来之后,将他们进行拼接,并且在中间加上换行符,然后将拼接后的数据添加到ListView的数据源里,并通知刷新一下ListView,最后将Cursor对象关闭掉,
注意要声明读取系统联系人的权限,修改AndroidManifest,xml的代码
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.contactstest"> <uses-permission android:name="android.permission.READ_CONTACTS"/> ...</manifest>
- android-ContentResolver
- android ContentResolver
- Android ContentResolver
- Android ContentResolver
- Android中的ContentResolver应用
- Android ContentResolver使用说明
- Android中的ContentResolver应用
- android ContentResolver详解
- Android 中的 ContentResolver
- android ContentResolver详解
- android ContentResolver详解
- android ContentResolver详解
- Android ContentResolver使用说明
- android中的ContentResolver
- android ContentResolver详解
- android ContentResolver详解
- android ContentResolver详解
- android ContentResolver详解
- Linux禁用Root远程登陆
- _01背包问题----简单粗暴
- centos7安装OTRS开源工单管理平台
- python编程实例-一个简单的购物车程序
- MySQL 更改数据文件路径
- Android ContentResolver
- Gamma 校正
- GAN相关论文
- codility AbsDistinct
- Androidstudio SVN 资源库 资源管理器 资源管理中心
- C# winfrom 中自定义的翻页控件(自己设计)
- mysql连接linux系统提示“登录出现1130 is not allowed to connect to this MySql server”问题,解决方法
- 时间格式化
- gcc编译选项(持续)