Android(X06HT Desire) 連絡先の取得と電話をかけるサンプル
テキストボックスに電話番号を入れてボタンを押すと、ダイヤル画面に遷移するサンプル。
コンタクトリストから、ユーザーID、表示名、電話番号をリストビューに表示し、選択されたらテキストボックスに電話番号を設定する。
- package info.typea.telephoneapp;
- import android.app.ListActivity;
- 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.EditText;
- import android.widget.LinearLayout;
- import android.widget.ListView;
- import android.widget.SimpleCursorAdapter;
- import android.widget.TextView;
- /**
- * @see http://stackoverflow.com/questions/1721279/how-to-read-contacts-on-android-2-0
- */
- public class TelephoneAppActivity extends ListActivity implements OnClickListener {
- /** Called when the activity is first created. */
- private EditText txtPhoneNo;
- private SimpleCursorAdapter adapter;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- txtPhoneNo = (EditText)findViewById(R.id.txt_phone_no);
- Button btn = (Button)findViewById(R.id.btn_call);
- btn.setOnClickListener(this);
- // リストに表示する項目
- String[] projection = {
- ContactsContract.CommonDataKinds.Phone._ID,
- ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
- ContactsContract.CommonDataKinds.Phone.NUMBER
- };
- String selection = null;
- String[] selectionArgs = null;
- String sortOrder = ContactsContract.PhoneLookup.DISPLAY_NAME + " asc";
- // カーソルの生成
- Cursor cur = managedQuery(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
- projection,
- selection, selectionArgs, sortOrder);
- // データベースのカラムとリストビューの関連づけ
- String from[] = new String[] { ContactsContract.CommonDataKinds.Phone._ID,
- ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
- ContactsContract.CommonDataKinds.Phone.NUMBER };
- int[] to = new int[] { R.id.lbl_id, R.id.lbl_name, R.id.lbl_phone};
- // リストアダプタのセット
- adapter = new SimpleCursorAdapter(this, R.layout.row_phoneno, cur, from, to);
- setListAdapter(adapter);
- }
- @Override
- protected void onListItemClick(ListView l, View v, int position, long id) {
- super.onListItemClick(l, v, position, id);
- LinearLayout ll = (LinearLayout) v;
- TextView phone = (TextView) ll.findViewById(R.id.lbl_phone);
- txtPhoneNo.setText(phone.getText());
- }
- @Override
- public void onClick(View v) {
- // ダイアルアクティビティに遷移
- String phoneNo = "tel:" + txtPhoneNo.getText().toString();
- Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(phoneNo));
- startActivity(intent);
- }
- }
/res/layout/main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <EditText android:text=""
- android:layout_height="wrap_content"
- android:layout_width="fill_parent"
- android:inputType="phone"
- android:id="@+id/txt_phone_no"></EditText>
- <Button android:id="@+id/btn_call"
- android:layout_height="wrap_content"
- android:layout_width="fill_parent"
- android:text="call"></Button>
- <ListView android:id="@+id/android:list"
- android:layout_height="wrap_content"
- android:layout_width="fill_parent"></ListView>
- <TextView android:text="No Contact list."
- android:id="@+id/android:empty"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"></TextView>
- </LinearLayout>
/res/layout/row_phoneno.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/LinearLayout01"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TextView android:id="@+id/lbl_id"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" >
- </TextView>
- <TextView android:text=" : "
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" >
- </TextView>
- <TextView android:id="@+id/lbl_name"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" >
- </TextView>
- <TextView android:text=" : "
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" >
- </TextView>
- <TextView android:id="@+id/lbl_phone"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" >
- </TextView>
- </LinearLayout>
あと、マニフェストで、以下のパーミッションを設定
- <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
- <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>