如何调用Android联系人列表?
我正在制作一个 Android 应用程序,需要调用手机的联系人列表。 我需要调用联系人列表功能,选择一个联系人,然后返回我的应用程序并显示该联系人的姓名。 这是我在互联网上获得的代码,但它不起作用。
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
public class Contacts extends ListActivity {
private ListAdapter mAdapter;
public TextView pbContact;
public static String PBCONTACT;
public static final int ACTIVITY_EDIT=1;
private static final int ACTIVITY_CREATE=0;
// Called when the activity is first created.
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Cursor C = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
startManagingCursor(C);
String[] columns = new String[] {People.NAME};
int[] names = new int[] {R.id.row_entry};
mAdapter = new SimpleCursorAdapter(this, R.layout.mycontacts, C, columns, names);
setListAdapter(mAdapter);
} // end onCreate()
// Called when contact is pressed
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Cursor C = (Cursor) mAdapter.getItem(position);
PBCONTACT = C.getString(C.getColumnIndex(People.NAME));
// RHS 05/06
//pbContact = (TextView) findViewById(R.id.myContact);
//pbContact.setText(new StringBuilder().append("b"));
Intent i = new Intent(this, NoteEdit.class);
startActivityForResult(i, ACTIVITY_CREATE);
}
}
I'm making an Android app, and need to call the phone's contact list. I need to call the contacts list function, pick a contact, then return to my app with the contact's name. Here's the code I got on the internet, but it doesnt work.
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
public class Contacts extends ListActivity {
private ListAdapter mAdapter;
public TextView pbContact;
public static String PBCONTACT;
public static final int ACTIVITY_EDIT=1;
private static final int ACTIVITY_CREATE=0;
// Called when the activity is first created.
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Cursor C = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
startManagingCursor(C);
String[] columns = new String[] {People.NAME};
int[] names = new int[] {R.id.row_entry};
mAdapter = new SimpleCursorAdapter(this, R.layout.mycontacts, C, columns, names);
setListAdapter(mAdapter);
} // end onCreate()
// Called when contact is pressed
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Cursor C = (Cursor) mAdapter.getItem(position);
PBCONTACT = C.getString(C.getColumnIndex(People.NAME));
// RHS 05/06
//pbContact = (TextView) findViewById(R.id.myContact);
//pbContact.setText(new StringBuilder().append("b"));
Intent i = new Intent(this, NoteEdit.class);
startActivityForResult(i, ACTIVITY_CREATE);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
我不是 100% 确定您的示例代码应该做什么,但以下代码片段应该可以帮助您“调用联系人列表函数,选择一个联系人,然后使用联系人的姓名返回[您的]应用程序”。
此过程分为三个步骤。
1. 权限
将读取联系人数据的权限添加到您的应用程序清单中。
2. 调用联系人选取器
在您的 Activity 中,创建一个 Intent,要求系统查找可以从联系人 URI 中的项目执行 PICK 操作的 Activity。
调用
startActivityForResult
,传入此 Intent(以及请求代码整数,本例中为PICK_CONTACT
)。 这将导致 Android 启动一个注册为支持People.CONTENT_URI
上的ACTION_PICK
的 Activity,然后在做出(或取消)选择时返回到此 Activity。3. 监听结果
同样在您的 Activity 中,重写
onActivityResult
方法以监听您在步骤 2 中启动的“选择联系人”Activity 的返回结果。您应该检查返回的请求代码是否匹配您期望的值,并且结果代码为RESULT_OK
。您可以通过对 data Intent 参数调用
getData()
来获取所选联系人的 URI。 要获取所选联系人的姓名,您需要使用该 URI 创建新查询并从返回的游标中提取姓名。完整源代码:教程- android.blogspot.com(如何调用android联系人列表).
I'm not 100% sure what your sample code is supposed to do, but the following snippet should help you 'call the contacts list function, pick a contact, then return to [your] app with the contact's name'.
There are three steps to this process.
1. Permissions
Add a permission to read contacts data to your application manifest.
2. Calling the Contact Picker
Within your Activity, create an Intent that asks the system to find an Activity that can perform a PICK action from the items in the Contacts URI.
Call
startActivityForResult
, passing in this Intent (and a request code integer,PICK_CONTACT
in this example). This will cause Android to launch an Activity that's registered to supportACTION_PICK
on thePeople.CONTENT_URI
, then return to this Activity when the selection is made (or canceled).3. Listening for the Result
Also in your Activity, override the
onActivityResult
method to listen for the return from the 'select a contact' Activity you launched in step 2. You should check that the returned request code matches the value you're expecting, and that the result code isRESULT_OK
.You can get the URI of the selected contact by calling
getData()
on the data Intent parameter. To get the name of the selected contact you need to use that URI to create a new query and extract the name from the returned cursor.Full source code: tutorials-android.blogspot.com (how to call android contacts list).
对于 Android 2.2 Froyo 版本,我这样做:
基本上使用 eclipse 创建一个类,例如:
public class SomePickContactName extends Activity
然后插入此代码。 请记住添加我的代码版本中引用的私有类变量和常量:
I do it this way for Android 2.2 Froyo release:
basically use eclipse to create a class like:
public class SomePickContactName extends Activity
then insert this code. Remember to add the private class variables and CONSTANTS referenced in my version of the code:
使用 ContactsContract API 寻找 API 级别 5 解决方案,您可以使用以下内容稍微修改上面的代码:
然后在 onActivityResult 中使用列名称:
Looking around for an API Level 5 solution using ContactsContract API you could slightly modify the code above with the following:
And then in onActivityResult use the column name:
这是获取联系方式的代码片段:
}
Here is the code snippet for get contact:
}
因为如果不选择任何联系人,就会出现异常。 所以最好检查一下这个情况。
because without selecting any contact it will give an exception. so better to check this condition.
完整代码如下
The complete code is given below
使用 Android 联系人列表时要小心。
上述方法中读取联系人列表适用于除 HTC One 和 Sony Xperia 之外的大多数 Android 设备。 浪费了我六周的时间试图找出问题所在!
大多数在线教程几乎都是相似的 - 首先读取“所有”联系人,然后使用
ArrayAdapter
在Listview
中显示。 这不是内存有效的解决方案。 与其先在其他网站上寻找解决方案,不如看看developer.android.com。 如果在developer.android.com 上没有任何解决方案,您应该寻找其他地方。解决方案是使用
CursorAdapter
而不是ArrayAdapter
来检索联系人列表。 使用 ArrayAdapter 可以在大多数设备上运行,但效率不高。 当ListView
滚动时,CursorAdapter
在运行时仅检索联系人列表的一部分。检索联系人列表:检索联系人列表
Be careful while working with the android contact list.
Reading the contact list in the above methods works on most android devices except HTC One and Sony Xperia. It wasted my six weeks trying to figure out what is wrong!
Most tutorials available online are almost similar - first read "ALL" contacts, then show in
Listview
withArrayAdapter
. This is not memory efficient solution. Instead of looking for solutions on other websites first, have a look at developer.android.com. If any solution is not available on developer.android.com you should look somewhere else.The solution is to use
CursorAdapter
instead ofArrayAdapter
for retrieving the contact list. UsingArrayAdapter
would work on most devices, but it's not efficient. TheCursorAdapter
retrieves only a portion of the contact list at run time while theListView
is being scrolled.Retrieving a List of Contacts: Retrieving a List of Contacts
令我惊讶的是,您不需要用户权限 CONTACT_READ 来读取姓名和一些基本信息(联系人是否已加星标,上次通话时间是什么时候)。
但是,您确实需要获得许可才能读取联系人的详细信息(例如电话号码)。
To my surprise, you do not need users-permission CONTACT_READ to read the names and some basic information (Is the contact starred, what was the last calling time).
However, you do need permission to read the details of the contact like the phone number.
我有一个代码可以通过共享首选项将联系人保存在您的数据库中
这是我的代码
I have a code to save the contact in your database by shared preference
here is my code
我使用@Colin MacKenzie - III 提供的代码。 多谢!
对于正在寻找“已弃用”的 ManagedQuery 替代品的人:
1st,假设使用 v4 支持库:
2nd:
3rd、
4th,覆盖回调:
5th:
6th,上面的代码,只不过我把签名参数从Intent改为Cursor:
7th,调用initLoader:
8th,别忘了这段代码
参考:
Android 基础知识:正确加载数据
在 Activity 中初始化加载程序
I use the code provided by @Colin MacKenzie - III. Thanks a lot!
For someone who are looking for a replacement of 'deprecated' managedQuery:
1st, assuming using v4 support lib:
2nd:
3rd,
4th, override callbacks:
5th:
6th, the code above, except that I change the signature param from Intent to Cursor:
7th, call initLoader:
8th, don't forget this piece of code
References:
Android Fundamentals: Properly Loading Data
Initializing a Loader in an Activity