如何检索所选联系人中的号码?

时间:2012-05-22 02:21:43

标签: android contacts

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ImportContactsActivity extends Activity {
/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button pickContact = (Button) findViewById(R.id.contacts);
        pickContact.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {

                Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
                startActivityForResult(intent, 1);
            }
        });
    }

    public void onActivityResult(int reqCode, int resultCode, Intent data) {

        super.onActivityResult(reqCode, resultCode, data);

        switch (reqCode) {
        case (1) :
          if (resultCode == Activity.RESULT_OK) {
              Uri contactData = data.getData();
              Cursor cursor =  managedQuery(contactData, null, null, null, null);
              ContentResolver cr = getContentResolver();
              Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, 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));
                      if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                          Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
                                             ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                                             new String[]{id}, null);
                          while (pCur.moveToNext()) {
                              // Do something with phones
                              TextView contactView = (TextView) findViewById(R.id.contactView);
                              contactView.setText(name.toString());
                      } 
                           pCur.close();
                      }
                  }
              }
              cursor.moveToFirst();
              String number = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.));

              TextView contactView = (TextView) findViewById(R.id.contactView);
              contactView.setText(number.toString());
          }
          break;
        }
    }
}

我要将手机通讯录导入应用程序,一旦用户点击按钮,手机联系人列表就会显示出来,一旦用户点击其中一个联系人,就会选择联系人,号码将会显示在textview上显示。但是,当我点击联系人时,发生了错误

  

java.lang.RuntimeException:传递结果失败   ResultInfo {who = null,request = 1,result = -1,data = Intent {   DAT =含量://com.android.contacts/contacts/lookup/0r1-3F4927292B/1   flg = 0x1}}到活动   {com.example.importcontacts / com.example.importcontacts.ImportContactsActivity}:   java.lang.SecurityException:Permission Denial:reading   com.android.providers.contacts.ContactsProvider2 uri   content://com.android.contacts/contacts from pid = 609,uid = 10053   需要android.permission.READ_CONTACTS

请告诉我编码,谢谢

1 个答案:

答案 0 :(得分:1)

好像忘了在清单文件中添加READ_CONTACTS权限

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