編輯:關於android開發
本文主要介紹android中怎樣獲取系統的聯系人數據
首先打開模擬器
點擊聯系人圖標按鈕
說明系統聯系人數據庫是空的,打開File explorer,找到data/data下面的文件夾:
將contacts2.db文件導出,添加到sqlite中的時候報錯:
點擊“ok”忽略之
表結構相當的復雜,首先搞清楚其中的三張表
* data表 保存聯系人的數據
* raw_contacts表 保存聯系人的id contact_id
* mimetypes表 保存聯系人數據的類型
接下來通過模擬器添加聯系人
通過sqlite刷新contacts2.db,忽略提示錯誤
獲取聯系人步驟
1. 查詢raw_contacts表,把聯系人的id取出來
2. 根據聯系人的id,查詢data表,把這個id的數據取出來
3. 根據mimetype區分數據的類型
接下來通過代碼實現
package com.wuyudong.getcontacts; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.ContentResolver; import android.database.Cursor; import android.view.Menu; import android.view.View; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } /** * 獲取全部的聯系人 * * @param view */ public void click(View view) { ContentResolver resolver = getContentResolver(); // 1.查詢raw_contacts表,把聯系人的id取出來 Uri uri = Uri.parse("content://com.android.contacts/raw_contacts"); Uri datauri = Uri.parse("content://com.android.contacts/data"); Cursor cursor = resolver.query(uri, new String[] { "contact_id" }, null, null, null); while (cursor.moveToNext()) { String id = cursor.getString(0); System.out.println("聯系人id: " + id); // 2.根據聯系人的id,查詢data表,把這個id的數據取出來 // 系統api查詢data表的時候不是真正的查詢的data表,而是查詢data表的視圖 Cursor dataCursor = resolver.query(datauri, new String[] { "data1", "mimetype" }, "raw_contact_id=?", new String[] { id }, null); while (dataCursor.moveToNext()) { String data1 = dataCursor.getString(0); System.out.println("data1=" + data1); String mimetype = dataCursor.getString(1); System.out.println("mimetype=" + mimetype); } dataCursor.close(); } cursor.close(); } }
添加權限:android.permission.READ_CONTACTS
運行後打印相關的結果:
06-18 10:59:41.556: I/System.out(2127): 聯系人id: 1
06-18 10:59:41.636: I/System.out(2127): data1=110
06-18 10:59:41.636: I/System.out(2127): mimetype=vnd.android.cursor.item/phone_v2
06-18 10:59:41.646: I/System.out(2127): [email protected]
06-18 10:59:41.646: I/System.out(2127): mimetype=vnd.android.cursor.item/email_v2
06-18 10:59:41.646: I/System.out(2127): data1=Wuyudong
06-18 10:59:41.646: I/System.out(2127): mimetype=vnd.android.cursor.item/name
通過上面的打印信息,可以修改程序
package com.wuyudong.getcontacts; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.ContentResolver; import android.database.Cursor; import android.view.Menu; import android.view.View; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } /** * 獲取全部的聯系人 * * @param view */ public void click(View view) { ContentResolver resolver = getContentResolver(); // 1.查詢raw_contacts表,把聯系人的id取出來 Uri uri = Uri.parse("content://com.android.contacts/raw_contacts"); Uri datauri = Uri.parse("content://com.android.contacts/data"); Cursor cursor = resolver.query(uri, new String[] { "contact_id" }, null, null, null); while (cursor.moveToNext()) { String id = cursor.getString(0); System.out.println("聯系人id: " + id); // 2.根據聯系人的id,查詢data表,把這個id的數據取出來 // 系統api查詢data表的時候不是真正的查詢的data表,而是查詢data表的視圖 Cursor dataCursor = resolver .query(datauri, new String[] { "data1", "mimetype" }, "raw_contact_id=?", new String[] { id }, null); while (dataCursor.moveToNext()) { String data1 = dataCursor.getString(0); String mimetype = dataCursor.getString(1); if ("vnd.android.cursor.item/name".equals(mimetype)) { System.out.println("姓名=" + data1); } else if ("vnd.android.cursor.item/email_v2".equals(mimetype)) { System.out.println("郵箱=" + data1); } else if ("vnd.android.cursor.item/phone_v2".equals(data1)) { System.out.println("電話=" + data1); } else if ("vnd.android.cursor.item/im".equals(mimetype)) { System.out.println("QQ=" + data1); } } dataCursor.close(); } cursor.close(); } }
運行後打印相關的結果:
06-18 11:17:57.312: I/System.out(2380): 聯系人id: 1
06-18 11:17:57.452: I/System.out(2380): 郵箱[email protected]
06-18 11:17:57.452: I/System.out(2380): 姓名=Wuyudong
06-18 11:17:57.452: I/System.out(2380): QQ=11111111
06-18 11:17:57.482: I/System.out(2380): 聯系人id: 2
06-18 11:17:57.572: I/System.out(2380): QQ=32423423422
06-18 11:17:57.572: I/System.out(2380): 郵箱[email protected]
06-18 11:17:57.572: I/System.out(2380): 姓名=Zhangsan
最後將這些操作封裝成api,便於以後使用
新建ContactInfoParser.java文件
package com.wuyudong.getcontacts.service; import java.util.ArrayList; import java.util.List; import com.wuyudong.getcontacts.domain.ContactInfo; import android.content.ContentResolver; import android.content.Context; import android.database.Cursor; import android.net.Uri; public class ContactInfoParser { /** * 獲取系統全部聯系人的api方法 * * @param context * @return */ public static List<ContactInfo> findAll(Context context) { ContentResolver resolver = context.getContentResolver(); // 1.查詢raw_contacts表,把聯系人的id取出來 Uri uri = Uri.parse("content://com.android.contacts/raw_contacts"); Uri datauri = Uri.parse("content://com.android.contacts/data"); List<ContactInfo> infos = new ArrayList<ContactInfo>(); Cursor cursor = resolver.query(uri, new String[] { "contact_id" }, null, null, null); while (cursor.moveToNext()) { String id = cursor.getString(0); if (id != null) { System.out.println("聯系人id: " + id); ContactInfo info = new ContactInfo(); info.setId(id); // 2.根據聯系人的id,查詢data表,把這個id的數據取出來 // 系統api查詢data表的時候不是真正的查詢的data表,而是查詢data表的視圖 Cursor dataCursor = resolver.query(datauri, new String[] { "data1", "mimetype" }, "raw_contact_id=?", new String[] { id }, null); while (dataCursor.moveToNext()) { String data1 = dataCursor.getString(0); String mimetype = dataCursor.getString(1); if ("vnd.android.cursor.item/name".equals(mimetype)) { System.out.println("姓名=" + data1); info.setName(data1); } else if ("vnd.android.cursor.item/email_v2" .equals(mimetype)) { System.out.println("郵箱=" + data1); info.setEmail(data1); } else if ("vnd.android.cursor.item/phone_v2".equals(data1)) { System.out.println("電話=" + data1); info.setPhone(data1); } else if ("vnd.android.cursor.item/im".equals(mimetype)) { System.out.println("QQ=" + data1); info.setQq(data1); } } infos.add(info); System.out.println("-------"); dataCursor.close(); } } cursor.close(); return infos; } }
新建ContactInfo.java
package com.wuyudong.getcontacts.domain; public class ContactInfo { private String name; private String id; private String phone; private String email; private String qq; @Override public String toString() { return "ContactInfo [name=" + name + ", id=" + id + ", phone=" + phone + ", email=" + email + ", qq=" + qq + "]"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getQq() { return qq; } public void setQq(String qq) { this.qq = qq; } }
最後調用相關api
package com.wuyudong.getcontacts; import java.util.List; import com.wuyudong.getcontacts.domain.ContactInfo; import com.wuyudong.getcontacts.service.ContactInfoParser; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.ContentResolver; import android.database.Cursor; import android.view.Menu; import android.view.View; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } /** * 獲取全部的聯系人 * * @param view */ public void click(View view) { List<ContactInfo> infos = ContactInfoParser.findAll(this); for (ContactInfo info : infos) { System.out.println(info.toString()); } } }
Android 用adb pull或push 拷貝手機文件到到電腦上,拷貝手機數據庫到電腦上,拷貝電腦數據庫到手機上,先說一下adb命令配置,如果遇到adb不是內部或外部
[轉]File Descriptor洩漏導致Crash: Too many open files,descriptorcrash在實際的Android開發過程中,我們遇到
Android簡單的ListViewDemo及每個控件的點擊事件,listview控件點擊事件 main.xml主頁面布局 <?xml version
以前曾經地介紹過MediaPlayer的基本用法,這裡就深入地講解MediaPlayer的在線播放