編輯:關於 Android 開發
讀取手機聯繫人是許多應用的常見需求,例如快速填寫聯絡資訊。本文透過一個完整範例,示範如何讀取系統聯繫人、以列表展示,並將使用者選擇的號碼回傳給上一個頁面。
GetPhoneActivity負責發起聯繫人選擇請求,並透過onActivityResult接收回傳的號碼:
public class GetPhoneActivity extends Activity {
private EditText editText;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.getphoneactivity);
editText = (EditText) findViewById(R.id.et_getphone);
button = (Button) findViewById(R.id.bb_getphone);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(GetPhoneActivity.this, ContactActivity.class);
startActivityForResult(intent, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
String phone = data.getStringExtra("phone");
// 去除號碼中的 "-" 與空格
phone = phone.replaceAll("-", "").replaceAll(" ", "");
editText.setText(phone); // 將號碼設置給輸入框
}
super.onActivityResult(requestCode, resultCode, data);
}
}
ContactActivity負責從系統讀取聯繫人,用ListView展示,並在用戶點擊時回傳所選號碼:
public class ContactActivity extends Activity {
private ListView lvList;
private ArrayList<HashMap<String, String>> readContact;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact);
lvList = (ListView) findViewById(R.id.lv_list);
readContact = readContact();
lvList.setAdapter(new SimpleAdapter(this, readContact, R.layout.contact_list_item,
new String[] { "name", "phone" },
new int[] { R.id.tv_name, R.id.tv_phone }));
lvList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String phone = readContact.get(position).get("phone");
Intent intent = new Intent();
intent.putExtra("phone", phone);
setResult(Activity.RESULT_OK, intent); // 回傳給上一頁
finish();
}
});
}
private ArrayList<HashMap<String, String>> readContact() {
// 先從raw_contacts讀取聯繫人id(contact_id)
// 再根據contact_id從data表查詢對應的號碼與名稱
// 透過mimetype區分哪個是名稱、哪個是號碼
Uri rawContactsUri = Uri.parse("content://com.android.contacts/raw_contacts");
Uri dataUri = Uri.parse("content://com.android.contacts/data");
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
Cursor rawContactsCursor = getContentResolver().query(
rawContactsUri, new String[] { "contact_id" }, null, null, null);
if (rawContactsCursor != null) {
while (rawContactsCursor.moveToNext()) {
String contactId = rawContactsCursor.getString(0);
Cursor dataCursor = getContentResolver().query(dataUri,
new String[] { "data1", "mimetype" },
"contact_id=?", new String[] { contactId }, null);
if (dataCursor != null) {
HashMap<String, String> map = new HashMap<String, String>();
while (dataCursor.moveToNext()) {
String data1 = dataCursor.getString(0);
String mimetype = dataCursor.getString(1);
if ("vnd.android.cursor.item/phone_v2".equals(mimetype)) {
map.put("phone", data1);
} else if ("vnd.android.cursor.item/name".equals(mimetype)) {
map.put("name", data1);
}
}
list.add(map);
dataCursor.close();
}
}
rawContactsCursor.close();
}
return list;
}
}
讀取聯繫人的核心在於兩張系統表:raw_contacts 存放聯繫人的contact_id,而 data 表則根據contact_id存儲具體的資料。由於同一個聯繫人可能同時包含名稱與多個號碼,因此需透過 mimetype 欄位來區分每一筆資料究竟是名稱(vnd.android.cursor.item/name)還是號碼(vnd.android.cursor.item/phone_v2)。掌握這個結構,就能靈活地讀取並組織聯繫人資訊。
Android 自定義多級聯動篩選控件:ToggleButton + PopupWindow 完整實現
電商、招聘、房產類 App 幾乎都離不開頂部的多級篩選欄:點一下按鈕,下方彈出選項列表,選完後按鈕文案變成選中值。本文記錄這樣一個自定義控件的完整實現思路。 效果如下:
CentOS 下 Apache 2.4 虛擬主機的三種配置方式:基於 IP、端口與域名
整體思路 苦於沒有足夠的生產環境服務器做實驗,不妨在虛擬機裡把 Apache 虛擬主機玩明白。Apache 2.4 支持三種虛擬主機模式: 方法一:基於 IP——IP1:
把 Android 專案打成 jar,並把依賴的 jar 一起合併進去
前言:關於 .jar 檔案 日常開發中經常會用到第三方的 .jar。其實 .jar 就是一個類似 .zip 的壓縮包,裡面裝的是程式碼——注意它不包含資源檔(res、圖片
Android觸摸事件傳遞機制全解析:從WMS一路走到View樹
Android觸摸事件傳遞機制全解析:從WMS一路走到View樹 轉眼快一年沒更新部落格了,工作一忙起來就很難擠出時間寫文章。由於目前正從事Android相關開發,往後的