效果圖
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiYWan5CO2kTO4EDNkFmMzUDN3QWOyYzX5EDOyETM2AzLcZDMyIDMy8CXn9Gbi9CXzV2Zh1WavwVbvNmLvR3YxUjLyM3Lc9CX6MHc0RHaiojIsJye.gif)
關鍵代碼
activity_pick_contact
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.a00xiaoyugmailcom.myapplication.ConnectionSelectActivity"
android:orientation="vertical"
android:padding="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="收貨人"/>
<EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="手機号碼"/>
<EditText
android:id="@+id/et_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:id="@+id/btn_select"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape_button_normal"
android:text="選聯系人"
android:textColor="#ff0000"
android:layout_marginTop="10dp"/>
</LinearLayout>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class ConnectionSelectActivity extends AppCompatActivity implements View.OnClickListener {
private EditText et_username;
private EditText et_phone;
private Button btn_select;
private static final int PICK_CONTACT = 1;
private static final int PERMISSIONS_REQUEST_READ_CONTACTS = 2;
private Intent mIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pick_contact);
et_username = findViewById(R.id.et_username);
et_phone = findViewById(R.id.et_phone);
btn_select = findViewById(R.id.btn_select);
btn_select.setOnClickListener(this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case PICK_CONTACT:
mIntent = data;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
//申請授權,第一個參數為要申請使用者授權的權限;第二個參數為requestCode 必須大于等于0,主要用于回調的時候檢測,比對特定的onRequestPermissionsResult。
//可以從方法名requestPermissions以及第二個參數看出,是支援一次性申請多個權限的,系統會通過對話框逐一詢問使用者是否授權。
requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, PERMISSIONS_REQUEST_READ_CONTACTS);
}else{
//如果該版本低于6.0,或者該權限已被授予,它則可以繼續讀取聯系人。
getContacts(data);
}
break;
default:
super.onActivityResult(requestCode, resultCode, data);
break;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == PERMISSIONS_REQUEST_READ_CONTACTS) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// 使用者成功授予權限
getContacts(mIntent);
} else {
Toast.makeText(this, "你拒絕了此應用對讀取聯系人權限的申請!", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onClick(View view) {
selectConnection();
}
private void selectConnection() {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
private void getContacts(Intent data) {
if (data == null) {
return;
}
Uri contactData = data.getData();
if (contactData == null) {
return;
}
String name = "";
String phoneNumber = "";
Uri contactUri = data.getData();
Cursor cursor = getContentResolver().query(contactUri, null, null, null, null);
if (cursor.moveToFirst()) {
name = cursor
.getString(cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String hasPhone = cursor
.getString(cursor
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
String id = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
if (hasPhone.equalsIgnoreCase("1")) {
hasPhone = "true";
} else {
hasPhone = "false";
}
if (Boolean.parseBoolean(hasPhone)) {
Cursor phones = getContentResolver()
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + id, null, null);
while (phones.moveToNext()) {
phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
}
cursor.close();
et_username.setText(name);
et_phone.setText(phoneNumber);
}
}
}