您的位置:首页 > 编程语言

第一行代码-7.2 访问其他程序中的数据

2016-02-22 15:12 453 查看
1、ContentResolver的用法

  这个和前面学习的SQLiteDatabase的用法特别相似。思路都是一样的,只是一些参数上面的不同。

(1)getContentResolver()和Uri

  在学习SQLite的时候,如果我们需要进行CRUD操作,就要通过databasehelper.getWritableDatabase或者getReadableDatabase。与之对应的是Context.getContentResolver。它不需要接收任何参数,但是接下来获取Cursor的query函数就要接收Uri参数了。

  Uri其实就相当于之前的表名,但是对格式方面有要求。它也被成为内容Uri。内容URI 给内容提供器中的数据建立了唯一标识符,它主要由两部分组成,权限(authority)和路径(path)。权限是用于对不同的应用程序做区分的,一般为了避免冲突,都会采用程序包名的方式来进行命名。比如某个程序的包名是com.example.app,那么该程序对应的权限就可以命名为com.example.app.provider。路径则是用于对同一应用程序中不同的表做区分的,通常都会添加到权限的后面。比如某个程序的数据库里存在两张表,table1 和table2,这时就可以将路径分别命名为/table1和/table2,然后把权限和路径进行组合,内容URI 就变成了com.example.app.provider/table1和com.example.app.provider/table2。不过,目前还很难辨认出这两个字符串就是两个内容URI,我们还需要在字符串的头部加上协议声明。因此,内容URI 最标准的格式写法如下:

content://com.example.app.provider/table1

content://com.example.app.provider/table2

  有没有发现,内容URI 可以非常清楚地表达出我们想要访问哪个程序中哪张表里的数据。也正是因此,ContentResolver 中的增删改查方法才都接收Uri 对象作为参数,因为使用表名的话系统将无法得知我们期望访问的是哪个应用程序里的表。

  在得到了内容URI 字符串之后,我们还需要将它解析成Uri 对象才可以作为参数传入。解析的方法也相当简单,代码如下所示:

Uri uri = Uri.parse(“content://com.example.app.provider/table1”)

  只需要调用Uri.parse()方法,就可以将内容URI 字符串解析成Uri 对象了。

  query还有其他几个参数,它们的含义如下:

query()方法参数对应SQL部分描述
urifrom table_name指定查询某个应用程序下的某一张表
projectionselect column1, column2指定查询的列名
selectionwhere column = value指定where 的约束条件
selectionArgs-为where 中的占位符提供具体的值
orderByorder by column1, column2指定查询结果的排序方式
(2)CRUD操作

// ContentResolver的CRUD操作
// 遍历
if (cursor != null) {
while (cursor.moveToNext()) {
String column1 = cursor.getString(cursor.getColumnIndex("column1"));
int column2 = cursor.getInt(cursor.getColumnIndex("column2"));
}
cursor.close();
}
// 插入
ContentValues values = new ContentValues();
values.put("column1", "text");
values.put("column2", 1);
getContentResolver().insert(uri, values);
// 更新
ContentValues values = new ContentValues();
values.put("column1", "");
getContentResolver().update(uri, values, "column1 = ? and column2 = ?", new
String[] {"text", "1"});
// 删除
getContentResolver().delete(uri, "column2 = ?", new String[] { "1" });


2、读取系统联系人

  需要记住ContactsContract的一些内置变量:

// MainActivity.java
private ListView contactsView;
private ArrayAdapter<String> adapter;
private List<String> contactsList = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactsView = (ListView) findViewById(R.id.contracts_listview);
adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, contactsList);
contactsView.setAdapter(adapter);
readContacts();
}

private void readContacts() {
Cursor cursor = null;
try {
cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null, 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);
}
}
catch (Exception e) {Log.e("sysu", "遍历联系人失败!");}
finally {
if (cursor != null) cursor.close();
}
}


<!-- Manifest -->
<uses-permission android:name="android.permission.READ_CONTACTS"/>


  实现效果:


     
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: