如何调用Android的联系人列表?联系人、列表、Android

2023-09-11 10:26:00 作者:离人未归

我在做一个Android应用程序,需要调用手机的联系人列表。我需要调用联系人列表功能,选择一个联系人,然后回到我的应用程序与联系人的姓名。这里的code我在互联网上,但它不工作。

 进口android.app.ListActivity;
进口android.content.Intent;
进口android.database.Cursor;
进口android.os.Bundle;
进口android.provider.Contacts.People;
进口android.view.View;
进口android.widget.ListAdapter;
进口android.widget.ListView;
进口android.widget.SimpleCursorAdapter;
进口android.widget.TextView;

公共类联系人扩展ListActivity {

    私人ListAdapter mAdapter;
    公众的TextView pbContact;
    公共静态字符串PBCONTACT;
    公共静态最终诠释ACTIVITY_EDIT = 1;
    私有静态最终诠释ACTIVITY_CREATE = 0;

    //第一次创建活动时调用。
    @覆盖
    公共无效的onCreate(包冰柱){
        super.onCreate(冰柱);
        光标C = getContentResolver()查询(People.CONTENT_URI,NULL,NULL,NULL,NULL);
        startManagingCursor(C);

        的String []列=新的String [] {People.NAME};
        INT []名=新INT [] {R.id.row_entry};

        mAdapter =新SimpleCursorAdapter(这一点,R.layout.mycontacts,C,列名);
        setListAdapter(mAdapter);
    } //结束的onCreate()
    //调用时接触是pressed
    @覆盖
    保护无效onListItemClick(ListView的L,视图V,INT位置,长的id){
        super.onListItemClick(L,V,位置ID);

        光标C =(光标)mAdapter.getItem(位置);
        PBCONTACT = C.getString(C.getColumnIndex(People.NAME));

        // RHS 05/06
        // pbContact =(TextView中)findViewById(R.id.myContact);
        。//pbContact.setText(new StringBuilder的()追加(B));

        意图I =新的意图(这一点,NoteEdit.class);
        startActivityForResult(ⅰ,ACTIVITY_CREATE);
    }
}
 

解决方案

我不是100%确定你的样品code是应该做的,但下面的代码片段应该可以帮助你拨打联系人列表功能,选择一个联系人,然后返回到[你]的应用程序与联系人的姓名。

有三个步骤,这个过程。

1)权限

添加到读取联系人数据到你的应用程序清单的权限。

 <使用-权限的Andr​​oid:名称=android.permission.READ_CONTACTS/>
 
读取android通讯录内容程序运行不成功

2)调用联系人选择器

在你的活动,创建要求系统发现,可以从执行中的项目联系人一挑动作的活动的意图URI。

 意向意图=新的意图(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
 

呼叫 startActivityForResult ,传递这种意图(和请求code整数, PICK_CONTACT 在这个例子中)。这将导致Android的推出是公司注册,以支持活动 ACTION_PICK People.CONTENT_URI ,然后返回此活动当做出选择(或取消)。

  startActivityForResult(意向,PICK_CONTACT);
 

3)听的结果

另外,在你的活动,覆盖 onActivityResult 方法监听从返回的选择联系人在步骤2中启动的活动,您应该检查返回请求code匹配你期待的价值,这个结果code是 RESULT_OK

您可以通过调用获取选定联系人的URI 的getData()上的数据的意图参数。为了让您需要使用URI来创建一个新的查询和提取的名字从返回光标选定联系人的名字。

  @覆盖
公共无效onActivityResult(INT REQ code,INT结果code,意图数据){
  super.onActivityResult(REQ code,因此code,数据);

  开关(REQ code){
    案例(PICK_CONTACT):
      如果(结果code == Activity.RESULT_OK){
        乌里联系数据= data.getData();
        光标C = getContentResolver()查询(联络人资料,NULL,NULL,NULL,NULL);
        如果(c.moveToFirst()){
          字符串名称= c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
          //不管你想要做的选定联系人姓名TODO。
        }
      }
      打破;
  }
}
 

全部来源点击这里:点击此处查看源代码

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);
    }
}

解决方案

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.

<uses-permission android:name="android.permission.READ_CONTACTS"/>

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.

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_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 support ACTION_PICK on the People.CONTENT_URI, then return to this Activity when the selection is made (or canceled).

startActivityForResult(intent, PICK_CONTACT);

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 is RESULT_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.

@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
  super.onActivityResult(reqCode, resultCode, data);

  switch (reqCode) {
    case (PICK_CONTACT) :
      if (resultCode == Activity.RESULT_OK) {
        Uri contactData = data.getData();
        Cursor c =  getContentResolver().query(contactData, null, null, null, null);
        if (c.moveToFirst()) {
          String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
          // TODO Whatever you want to do with the selected contact name.
        }
      }
      break;
  }
}

Full Source Click Here : Click here For Source