<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="40dp" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom" android:paddingLeft="10dp" android:text="姓名:" android:textColor="@android:color/black" android:textSize="13sp" /> <EditText android:id="@+id/et_name" android:layout_width="200dp" android:layout_height="fill_parent" android:layout_marginLeft="10dp" /> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="40dp" android:text="点击" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="40dp" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom" android:paddingLeft="10dp" android:text="电话:" android:textColor="@android:color/black" android:textSize="13sp" /> <EditText android:id="@+id/et_phone" android:layout_width="200dp" android:layout_height="fill_parent" android:layout_marginLeft="10dp" android:inputType="phone" /> </LinearLayout> </LinearLayout>
/**
* 获取联系人电话
*
* @param cursor
* @param context
* @return
*/
private ContactBen getContactPhone(Cursor cursor, Context context) {
ContactBen vo = new ContactBen();
int phoneColumn = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
int phoneNum = 0;
try {
phoneNum = cursor.getInt(phoneColumn);
} catch (Exception e) {
return null;
}
// String phoneResult = "";
if (phoneNum > 0) {
// 获得联系人的ID号
int idColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID);
String contactId = cursor.getString(idColumn);
vo.name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// 获得联系人的电话号码的cursor;
Cursor phones = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
if (phones.moveToFirst()) {
// 遍历所有的电话号码
for (; !phones.isAfterLast(); phones.moveToNext()) {
int index = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int typeindex = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
int phone_type = phones.getInt(typeindex);
String phoneNumber = phones.getString(index);
switch (phone_type) {
case 2:
vo.phone = phoneNumber;
break;
}
}
if (!phones.isClosed()) {
phones.close();
}
}
}
return vo;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case (1): {
if (resultCode == Activity.RESULT_OK) {
if (data != null) {
Uri contactData = data.getData();
@SuppressWarnings("deprecation")
Cursor c = MainActivity.this.managedQuery(contactData, null, null, null, null);
c.moveToFirst();
ContactBen contactPhone = getContactPhone(c, MainActivity.this);
if (contactPhone == null) {
contactPhone = new ContactBen();
}
et_name.setText("" + contactPhone.name);
et_phone.setText("" + contactPhone.phone);
}
}
break;
}
}
}
findViewById(R.id.btn1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
requestPermission(new String[] { Manifest.permission.READ_CONTACTS }, new PermissionHandler() {
@Override
public void onGranted() {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 1);
}
@Override
public void onDenied() {
super.onDenied();
}
});
}
});
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有