歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android--取得多組電話號碼/Activity.startManagingCursor方法的使用

Android--取得多組電話號碼/Activity.startManagingCursor方法的使用

日期:2017/3/1 10:01:58   编辑:Linux編程

我們知道,在添加聯系人的時候,可能一個聯系人不止一個號碼,這時我們需要一個取得聯系人多組號碼的程序。

首先,需要介紹兩點:

1.需要在AndroidManifest.xml文件中進行聲明

<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>

2.Activity.startManagingCursor方法

我們將獲得的Cursor對象交與Activity 來管理,這樣Cursor對象的生命周期便能與當前的Activity自動同步,省去了自己管理Cursor。

2.1.這個方法使用的前提是:游標結果集裡有很多的數據記錄。

所以,在使用之前,先對Cursor是否為null進行判斷,如果Cursor != null,再使用此方法

2.2.如果使用這個方法,最後也要用stopManagingCursor()來把它停止掉,以免出現錯誤。

2.3.使用這個方法的目的是把獲取的Cursor對象交給Activity管理,這樣Cursor的生命周期便能和Activity自動同步,

省去自己手動管理。

下面給出程序的實現截圖:

接下來給出程序的完整實現代碼:

public class EX06_20 extends ListActivity
{
/*
* 使用List的一般原因是Adapter中的內容有變化,如果是ArrayAdapter則不允許內容有變化
*/
private ListAdapter mListAdapter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

/* 取得通訊錄裡的數據 */
Cursor cursor = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
/* 取得筆數 */
int c = cursor.getCount();
if (c == 0)
{
Toast.makeText(EX06_20.this, "聯系人無資料\n請添加聯系人資料", Toast.LENGTH_LONG)
.show();
}

/* 用Activity管理Cursor */
startManagingCursor(cursor);

/* 欲顯示的字段名稱 */
String[] columns =
{ ContactsContract.Contacts.DISPLAY_NAME };

/* 欲顯示字段名稱的view */
int[] entries =
{ android.R.id.text1 };

mListAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, cursor, columns, entries);
/* 設置Adapter */
setListAdapter(mListAdapter);

}

@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
// TODO Auto-generated method stub

/* 取得點擊的Cursor */
Cursor c = (Cursor) mListAdapter.getItem(position);

/* 取得_id這個字段得值 */
int contactId = c.getInt(c.getColumnIndex(ContactsContract.Contacts._ID));

/* 用_id去查詢電話的Cursor */
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
null, null);

StringBuffer sb = new StringBuffer();
int type, typeLabelResource;
String number;

if (phones.getCount() > 0)
{

/* 2.0可以允許User設定多組電話號碼,依序撈出 */
while (phones.moveToNext())
{
/* 取得電話的TYPE */
type = phones.getInt(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
/* 取得電話號碼 */
number = phones.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
/* 由電話的TYPE找出LabelResource */
typeLabelResource = ContactsContract.CommonDataKinds.Phone
.getTypeLabelResource(type);

sb.append(getString(typeLabelResource) + ": " + number + "\n");

}
} else
{
sb.append("no Phone number found");
}

Toast.makeText(this, sb.toString(), Toast.LENGTH_SHORT).show();

super.onListItemClick(l, v, position, id);
}

}

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved