知识库

wiki.linsakura.com

用户工具

站点工具


wiki:book:notes:android:第一行代码-android-第2版-郭霖:第7章

差别

这里会显示出您选择的修订版和当前版本之间的差别。

到此差别页面的链接

两侧同时换到之前的修订记录 前一修订版
后一修订版
前一修订版
wiki:book:notes:android:第一行代码-android-第2版-郭霖:第7章 [2022/04/07 11:05]
博丽幻月 [增删改查table1]
wiki:book:notes:android:第一行代码-android-第2版-郭霖:第7章 [2022/04/07 14:39] (当前版本)
博丽幻月 [创建自己的内容提供器]
行 50: 行 50:
 Uri uri = Uri.parse("content://com.example.app.provider/table1"); Uri uri = Uri.parse("content://com.example.app.provider/table1");
 </code> </code>
-=== 增删改查table1 === +=== query() ===
-**query table1**\\+
 <code java> <code java>
 Cursor cursor = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder); Cursor cursor = getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
行 65: 行 64:
 } }
 </code> </code>
-**insert table1**\\+=== insert() ===
 <code java> <code java>
 ContentValues values= new ContentValues(); ContentValues values= new ContentValues();
行 72: 行 71:
 getContentResolver().insert(uri, values); getContentResolver().insert(uri, values);
 </code> </code>
-**update table1**\\+=== update() ===
 <code java> <code java>
 ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
行 78: 行 77:
 getContentResolver().update(uri, values, "column1 = ? and column2 = ?", new String[]{ "text", "1" }); getContentResolver().update(uri, values, "column1 = ? and column2 = ?", new String[]{ "text", "1" });
 </code> </code>
-**delete table1**\\+=== delete() ===
 <code java> <code java>
 getContentResolver().delete(uri, "column2 = ?", new String[]{ "1" }); getContentResolver().delete(uri, "column2 = ?", new String[]{ "1" });
 </code> </code>
 +==== 读取系统联系人 ====
 +读取联系人方法:\\
 +<code java>
 +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));
 +            }
 +        }
 +    } catch (Exception e) {
 +        e.printStackTrace();
 +    } finally {
 +        if (cursor != null) {
 +            cursor.close();
 +        }
 +    }
 +}
 +</code>
 +判断是否有权限和读取:\\
 +<code java>
 +if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
 +    ActivityCompat.requestPermissions(this, new String[]{ Manifest.permission.READ_CONTACTS }, 1);
 +} else {
 +    readContacts();
 +}
 +</code>
 +===== 创建自己的内容提供器 =====
 +==== 创建内容提供器步骤 ====
 +**TBD**\\
wiki/book/notes/android/第一行代码-android-第2版-郭霖/第7章.1649300751.txt.gz · 最后更改: 2022/04/07 11:05 由 博丽幻月