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

我要将手机联系人导入应用程序,一旦用户点击按钮,手机联系人列表将显示,一旦用户点击其中一个联系人,联系人被选中,号码将显示在文本视图。但是当我点击联系人时,发生了错误

java.lang.RuntimeException: 传递结果失败 ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/0r1-3F4927292B/1 flg = 0x1 }} 到活动 {com.example.importcontacts/com.example.importcontacts.ImportContactsActivity}:java.lang.SecurityException:权限拒绝:读取 com.android.providers.contacts.ContactsProvider2 uri content://com.android。来自 pid=609、uid=10053 的联系人/联系人需要 android.permission.READ_CONTACTS

请在编码方面给我建议,谢谢

4

1 回答 1

1

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

<uses-permission
    android:name="android.permission.READ_CONTACTS" />
于 2012-05-22T02:27:24.317 回答