Android獲取手機(jī)聯(lián)系人電話號(hào)碼并返回結(jié)果
之前做了一個(gè)小練習(xí)需要用到獲取手機(jī)里聯(lián)系人的電話號(hào)碼,通過查閱相關(guān)的資料,自己嘗試寫了一個(gè)小例子,可以成功獲取。
首先需要在AndroidManifest.xml中獲取權(quán)限
<uses-permission android:name="android.permission.READ_CONTACTS"/>
xml布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="電話號(hào)碼是:"/> <EditText android:id="@+id/editNumber" android:layout_width="match_parent" android:layout_height="wrap_content" android:editable="false"/> <Button android:id="@+id/buttonQuary" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="查詢聯(lián)系人"/> </LinearLayout>
MainActivity.java
package com.getcontacts;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
Button button;
EditText editText;
private Uri uri_DATA = Uri.parse("content://com.android.contacts/data");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.buttonQuary);
editText = (EditText) findViewById(R.id.editNumber);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
editText.setText(""); //做跳轉(zhuǎn)之前先把電話號(hào)碼清空
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1) { //請(qǐng)求碼為1
Log.i("info","resultADD");
String strNumber = "";
if(data != null) { //判斷返回的intent是不是為空
Uri uri = data.getData();
Log.i("info", uri.toString()); //在log打印出來獲取的uri
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
while(cursor.moveToNext()) {
String strID = cursor.getString(cursor.getColumnIndex("name_raw_contact_id"));
Cursor cursor2 = getContentResolver().query(uri_DATA, null,
"raw_contact_id = " + strID + " and mimetype_id = 5", null, null);
if(cursor2.moveToFirst()) {
strNumber = cursor2.getString(cursor2.getColumnIndex("data1"));
strNumber = strNumber.replace(" ", "");
strNumber = strNumber.replace("-", "");
}
cursor2.close();
}
cursor.close();
editText.setText(strNumber);
}
}
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android獲取手機(jī)通訊錄、sim卡聯(lián)系人及調(diào)用撥號(hào)界面方法
- Android 獲取手機(jī)聯(lián)系人實(shí)例代碼詳解
- android利用ContentResolver訪問者獲取手機(jī)聯(lián)系人信息
- Android獲取手機(jī)聯(lián)系人信息
- Android讀取手機(jī)通訊錄聯(lián)系人到自己項(xiàng)目
- android如何獲取手機(jī)聯(lián)系人的數(shù)據(jù)庫示例代碼
- Android編程實(shí)現(xiàn)讀取手機(jī)聯(lián)系人、撥號(hào)、發(fā)送短信及長按菜單操作方法實(shí)例小結(jié)
- 淺談Android手機(jī)聯(lián)系人開發(fā)之增刪查改功能
- Android ContentProvider獲取手機(jī)聯(lián)系人實(shí)例
- Android ContentProvider實(shí)現(xiàn)手機(jī)聯(lián)系人讀取和插入
相關(guān)文章
Android實(shí)現(xiàn)未讀消息小紅點(diǎn)顯示實(shí)例
大家好,本篇文章主要講的是Android實(shí)現(xiàn)未讀消息小紅點(diǎn)顯示實(shí)例,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下2022-02-02
Android遞歸方式刪除某文件夾下的所有文件(.mp3文件等等)
以刪除為例,當(dāng)然,對(duì)于遍歷某文件夾下的所有文件均可用這個(gè)方法。如搜索.mp3文件等,具體實(shí)現(xiàn)如下,感興趣的朋友可以參考下哈2013-06-06
Android 使用AlarmManager和NotificationManager來實(shí)現(xiàn)鬧鐘和通知欄
這篇文章主要介紹了Android 使用AlarmManager和NotificationManager來實(shí)現(xiàn)鬧鐘和通知欄,需要的朋友可以參考下2017-02-02
Android10填坑適配指南(實(shí)際經(jīng)驗(yàn)代碼)
這篇文章主要介紹了Android10填坑適配指南(實(shí)際經(jīng)驗(yàn)代碼),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
Android編程實(shí)現(xiàn)在Bitmap上涂鴉效果
這篇文章主要介紹了Android編程實(shí)現(xiàn)在Bitmap上涂鴉效果的方法,涉及Android界面布局,事件響應(yīng)及Bitmap操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-12-12
詳解基于Android App 安全登錄認(rèn)證解決方案
這篇文章主要介紹了基于Android App 安全登錄認(rèn)證解決方案,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09

