編輯:關於 Android 程式設計
在跟隨某個教學實作時,它選取聯絡人的方式是自行從資料庫讀出聯絡人、再用 ListView 顯示,選擇後返回手機號碼。但這樣做有個問題:點擊選擇時需要等待載入,且顯示出來的聯絡人似乎並不齊全,整體體驗不佳。
於是筆者改用呼叫系統聯絡人的方式來選擇,省去自行處理界面的麻煩,成果相當不錯。
package jason.pickcontact;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button pick;
TextView show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pick = (Button) findViewById(R.id.pick);
show = (TextView) findViewById(R.id.show);
pick.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 呼叫系統聯絡人
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
MainActivity.this.startActivityForResult(intent, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
if (resultCode == RESULT_OK) {
Uri contactData = data.getData();
Cursor cursor = managedQuery(contactData, null, null, null, null);
cursor.moveToFirst();
String num = this.getContactPhone(cursor);
show.setText("所選手機號為:" + num);
}
break;
}
}
private String getContactPhone(Cursor cursor) {
int phoneColumn = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
int phoneNum = cursor.getInt(phoneColumn);
String result = "";
if (phoneNum > 0) {
// 取得聯絡人的 ID
int idColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID);
String contactId = cursor.getString(idColumn);
// 取得聯絡人電話的 cursor
Cursor phone = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId,
null, null);
if (phone.moveToFirst()) {
for (; !phone.isAfterLast(); phone.moveToNext()) {
int index = phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String phoneNumber = phone.getString(index);
result = phoneNumber;
}
if (!phone.isClosed()) {
phone.close();
}
}
}
return result;
}
}
聯絡人中儲存號碼的類型不同,對應的 phone_type 也不同。一般來說手機那一欄的類型是 2,但實測發現用 2 反而有些選不出來——查看聯絡人後才發現,很多人並沒有把號碼存到手機欄,而是存成了電話或其他類型。
實測結果顯示,不同系統的類型對應有些差異:MIUI 系統下,手機欄的 TYPE 是 2,住宅是 3 和 1,總機是 12 和 7,其他是 7;而在模擬器的原生系統上,mobile 是 2、home 是 1、work 是 3、other 是 7。可見不同 ROM 之間存在差別,不過手機為 2 應該是通用的。由於筆者儲存的類型不齊全,程式碼中暫且註解掉了類型判斷直接使用;標準做法應加上類型 2 的判斷較為嚴謹。
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/pick"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/show"
android:layout_below="@+id/show"
android:text="選擇聯絡人" />
</RelativeLayout>
Android BroadcastReceiver 廣播接收器詳解
BroadcastReceiver 是什麼BroadcastReceiver 是 Android 四大元件之一,本質上是一個全域監聽器,用來接收系統或應用送出的廣播訊息。
Android WebView 快取機制與設定
WebView 快取能保存 HTML、JS、CSS 與其他資源,讓網頁在離線或網路不佳時仍有機會顯示。Android 的 WebView 快取可分為頁面快取與資料快取。快
Android 獲取系統自帶瀏覽器書簽的實現與思考
剛接手一個備份系統瀏覽器書簽的模塊,先把代碼貼出來,同時也記錄幾個自己的疑問,希望路過的高手能指點一二。 疑問一:根據官方 API,本應有以下幾個字段可以獲取: 但實際
PullToRefresh 下拉刷新庫源碼剖析:以 ListView 為例
PullToRefresh 是 GitHub 上被使用得非常廣泛的下拉刷新庫。今天來拆解它的源碼實作,以 ListView 為例,其他的元件(GridView、Scrol