Android--取得多组电话号码/Activity.startManagingCursor方法的使用

   我们知道,在添加联系人的时候,可能一个联系人不止一个号码,这时我们需要一个取得联系人多组号码的程序。

首先,需要介绍两点:

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);
  }

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值