Android應用開發揭秘裡的EX_03_02擷取連絡人號碼的擷取方法的修正

來源:互聯網
上載者:User

直接上源碼:

package com.example.contact;<br />import android.app.Activity;<br />import android.content.ContentResolver;<br />import android.database.Cursor;<br />import android.os.Bundle;<br />import android.provider.ContactsContract;<br />import android.provider.ContactsContract.PhoneLookup;<br />import android.util.Log;<br />import android.widget.TextView;<br />public class ReadContack extends Activity {<br /> /** Called when the activity is first created. */<br />@Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> TextView tv = new TextView(this);<br /> String string="";<br /> super.onCreate(savedInstanceState);<br /> ContentResolver cr = getContentResolver();<br /> Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI,<br /> null, null, null, null);<br /> while(cursor.moveToNext()) {<br /> int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);<br /> String contact = cursor.getString(nameFieldColumnIndex);</p><p> string += contact;<br /> string += ":";<br /> //下面為新的擷取連絡人電話號碼方法<br /> //擷取連絡人的ID號<br /> String contactid = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));<br /> Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,<br /> ContactsContract.CommonDataKinds.Phone.CONTACT_ID+'='+contactid,<br /> null, null);<br /> //電話號碼可以是幾組<br /> while(phone.moveToNext()) {<br /> String strPhoneNumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));<br /> string += strPhoneNumber;<br /> }<br /> string += "/n";<br /> phone.close();<br /> }<br /> cursor.close();<br /> tv.setText(string);<br /> setContentView(tv);<br /> }<br />}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.