是否可以选择特定联系人的电话号码?(Android)
Posted
技术标签:
【中文标题】是否可以选择特定联系人的电话号码?(Android)【英文标题】:Is it possible to pick a phone number of a specific contact?(Android) 【发布时间】:2014-01-19 02:13:54 【问题描述】:我想允许用户通过 startActivityForResult() 方法在我的应用程序中选择特定联系人的电话号码。可能吗? 例如,如果联系人有家庭电话号码和手机号码,则用户可以选择其中一个。
以下代码允许用户选择一个联系人:
Intent picker = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(picker, 53253);
以下代码允许用户从任何联系人(而不是特定联系人)中选择电话号码:
Intent picker = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(picker, 53253);
【问题讨论】:
无论你已经尝试过什么,也包括该代码 【参考方案1】:使用此代码在 Txt 文件中获取联系人
这是我的代码,它会帮助你
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity
TextView textDetail;
StringBuffer sb;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textDetail = (TextView) findViewById(R.id.textView1);
readContacts();
public void readContacts()
sb = new StringBuffer();
////sb.append("......Contact Details.....");
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
String phone = null;
String emailContact = null;
String emailType = null;
String image_uri = "";
Bitmap bitmap = null;
if (cur.getCount() > 0)
while (cur.moveToNext())
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
image_uri = cur
.getString(cur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
if (Integer
.parseInt(cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
System.out.println("name : " + name + ", ID : " + id);
sb.append("\nContact Name:" + name+" ");
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] id , null);
while (pCur.moveToNext())
phone = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
sb.append("\nPhone number:" + phone+" ");
System.out.println("phone" + phone);
pCur.close();
Cursor emailCur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = ?", new String[] id , null);
while (emailCur.moveToNext())
emailContact = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
emailType = emailCur
.getString(emailCur
.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
sb.append("\nEmail:" + emailContact + "Email type:" + emailType);
System.out.println("Email " + emailContact
+ " Email Type : " + emailType);
emailCur.close();
if (image_uri != null)
System.out.println(Uri.parse(image_uri));
try
bitmap = MediaStore.Images.Media
.getBitmap(this.getContentResolver(),
Uri.parse(image_uri));
sb.append("\n Image in Bitmap:" + bitmap);
System.out.println(bitmap);
catch (FileNotFoundException e)
// TODO Auto-generated catch block
e.printStackTrace();
catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
//sb.append("\n........................................");
textDetail.setText(sb);
File logFile = new File("sdcard/log.txt");
if (!logFile.exists())
try
logFile.createNewFile();
catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
try
/////BufferedWriter for performance, true to set append to file flag
BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
buf.append(sb);
buf.newLine();
buf.close();
catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
在 Mani fest 中使用权限
<uses-permission android:name="android.permission.READ_CONTACTS" />
<?xml version="1.0" encoding="utf-8" ?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.contact" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme">
- <activity android:name="com.example.contact.MainActivity" android:label="@string/app_name">
- <intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
【讨论】:
我知道如何通过查询获取联系人的电话号码,我想让用户从 startActivityForResult() 方法中选择其中一个。以上是关于是否可以选择特定联系人的电话号码?(Android)的主要内容,如果未能解决你的问题,请参考以下文章
使用 ContentProvider 从联系人获取电话号码 - Android
Android ACTION_PICK 特定联系人的电话号码