android获取电话号码实例
16lz
2021-01-23
在Androidmanifest注册获取权限<uses-permission android:name="android.permission.READ_CONTACTS"/>
源码如下:
- 欢迎加入QQ交流3群:317874559
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;@Overrideprotected 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() {@Overridepublic 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);}});}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubsuper.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);}}}}@Overridepublic 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;}}
<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" 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>
更多相关文章
- android中读取联系人和通话记录
- android 存储联系人(save contact) 总结
- (转)Android Contacts(一)—— 读取联系人
- 在Android中根据联系人查询电话号码
- Android联系人读取操作笔记
- Android--Intent常用(拨号,浏览器,联系人,Wi-Fi)
- android获取手机通讯录联系人
- 装载 联系人2.0的相关处理