Android獲取電話號碼實例
在Androidmanifest注冊獲取權限<uses-permission android:name="android.permission.READ_CONTACTS"/>
源碼如下:
package com.example.intentphone;import android.app.Activity; import android.content.CursorLoader; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.ContactsContract; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { EditText ed1; EditText ed2; Button bt1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ed1 = (EditText) findViewById(R.id.ed1); ed2 = (EditText) findViewById(R.id.ed2); bt1 = (Button) findViewById(R.id.bt1); // 啟動系統應用程序組件 bt1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // 創建Intent對象 Intent intent = new Intent(); // 設置Intent的Action屬性 intent.setAction(Intent.ACTION_GET_CONTENT); // 顯示指定Intent的數據類型 intent.setType("vnd.android.cursor.item/phone"); // 啟動系統Activity,請求碼為零 startActivityForResult(intent, 0); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if (requestCode == 0) { if (resultCode == Activity.RESULT_OK) { // 獲取返回的數據 Uri contactData = data.getData(); CursorLoader cursorLoader = new CursorLoader(this, contactData, null, null, null, null); // 查詢聯系人信息 Cursor cursor = cursorLoader.loadInBackground(); // 如果查詢到指定的聯系人 if (cursor.moveToFirst()) { String contactId = cursor.getString(cursor .getColumnIndex(ContactsContract.Contacts._ID)); // 獲取指定的聯系人查詢該聯系人的信息 String name = cursor .getString(cursor .getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); String phoneNumbere = "此聯系人暫時未輸入電話號碼"; Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId, null, null); if (phones.moveToFirst()) { // 取出電話號碼 phoneNumbere = phones .getString(phones .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); } //關閉游標 phones.close(); //顯示聯系人與聯系電話 ed1.setText(name); ed2.setText(phoneNumbere); } } } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } </pre><a class="CopyToClipboard" title="copy" href="/misc/goto?guid=4959550711177523549"></a></div>
</div> </div>
<LinearLayout xmlns:android=" xmlns:tools=" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" ><EditText android:id="@+id/ed1" android:layout_width="match_parent" android:layout_height="wrap_content" android:cursorVisible="false" android:editable="false" /> <EditText android:id="@+id/ed2" android:layout_width="match_parent" android:layout_height="wrap_content" android:cursorVisible="false" android:editable="false" /> <Button android:id="@+id/bt1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="查看聯系人" />
</LinearLayout> </pre></div> </div> </div> 來自:http://blog.csdn.net/x605940745/article/details/15339231
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!相關資訊
相關文檔
目錄