类微信联系人板块调用手机联系人
要求:
1.使用RecyclerView;
2.有一个ImageButton能实现添加联系人到手机联系人。
类微信界面+RecyclerView的使用见之前的博客:
链接: https://blog.csdn.net/qq_44841678/article/details/105080025.
注意点:
此程序需要得到读取和更改联系人数据的permission,用在清单文件里注册的方法来配置;
准备:
在清单文件AndroidManifest.xml里,添加两行:
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
实现界面展示:
在之前RecyclerView基础上主要修改部分:
.xml文件:
在top.xml中加一个ImageButton,同时LinearLayout的orientation属性改为horizontal。
<ImageButton
android:id="@+id/id_add_img"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="110dp"
android:background="#00000000"
android:clickable="false"
android:src="@drawable/add" />
新增一个add_contact.xml布局文件,再点击ImageButton之后以一个对话框的形式显示出来。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/name"
/>
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/phone"
/>
<EditText
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone" />
</LinearLayout>
事件监听控制:
contactFragment.java:
数据的获取要从库中调通讯录中的联系人和联系电话。这就首先得把原来存数据的的List< String>list;变为List<Map<String,String>>data;的形式。
数据获取函数:
private void initexpandData(){
Uri uri= ContactsContract.Contacts.CONTENT_URI;
ContentResolver resolver = context.getContentResolver();
Cursor cursor=resolver.query(uri, null, null, null, null); //得到记录集
if(cursor!=null){
while(cursor.moveToNext()){
//先获取联系人_id字段的索引号后再获取_id值
int idFieldIndex=cursor.getColumnIndex("_id");
int id=cursor.getInt(idFieldIndex);
//先获取联系人姓名字段的索引号后再获取姓名字段值
int nameFieldIndex = cursor.getColumnIndex("display_name");
String name=cursor.getString(nameFieldIndex);
int numCountFieldIndex=cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
int numCount=cursor.getInt(numCountFieldIndex); //获取联系人的电话号码个数
String phoneNumber="";
if(numCount>0){ //联系人有至少一个电话号码
//在类ContactsContract.CommonDataKinds.Phone中根据id查询相应联系人的所有电话;
Cursor phonecursor=getActivity().getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"=?",
new String[]{Integer.toString(id)}, null);
if(phonecursor!=null){
if(phonecursor.moveToFirst()){ //仅读取第一个电话号码
int numFieldIndex=phonecursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
phoneNumber=phonecursor.getString(numFieldIndex);
}
phonecursor.close();
}
}
item=new HashMap<String,String>(); //必须循环创建
item.put("name", name);
item.put("phoneNumber", phoneNumber);
data.add(item);
}
cursor.close();
}
}
MainActivity.java:
要添加授权监听函数,以及新增的ImageButton点击事件监听函数。在点击ImageButton之后打开一个AlertDialog,AlertDialog中有一个确定键,监听“确定”的点击事件,点击事件发生后,从打开的AlertDialog中的EditText获取数据,存入通讯录。
部分代码:
case R.id.id_add_img:
final View addDialog=getLayoutInflater().inflate(R.layout.add_contact,null);
new AlertDialog.Builder(MainActivity.this).setView(addDialog).setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
EditText mname=(EditText)addDialog.findViewById(R.id.name);
EditText mphone=(EditText)addDialog.findViewById(R.id.phone);
final String name=mname.getText().toString();
final String phone=mphone.getText().toString();
// 创建一个空的ContentValues
ContentValues values = new ContentValues();
// 向RawContacts.CONTENT_URI执行一个空值插入
// 目的是获取系统返回的rawContactId
Uri rawContactUri = getContentResolver().insert(
ContactsContract.RawContacts.CONTENT_URI, values);
long rawContactId = ContentUris.parseId(rawContactUri);
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
// 设置内容类型
values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
// 设置联系人名字
values.put(StructuredName.GIVEN_NAME, name);
// 向联系人URI添加联系人名字
getContentResolver().insert(ContactsContract
.Data.CONTENT_URI, values);
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
// 设置联系人的电话号码
values.put(Phone.NUMBER, phone);
// 设置电话类型
values.put(Phone.TYPE, Phone.TYPE_MOBILE);
// 向联系人电话号码URI添加电话号码
getContentResolver().insert(ContactsContract
.Data.CONTENT_URI, values);
values.clear();
selectFragment(2);
Toast.makeText(MainActivity.this, "联系人数据添加成功",
Toast.LENGTH_SHORT).show();
}
}).show();
break;
注意:
在MainActivity中需要添加一个授权监听函数。同时onCreate中也要添加相应代码。
本代码有一点缺陷在于,在点击“确定”往通讯录中添加了联系人之后,类微信界面的联系人Fragment如果不刷新则不会显示刚刚添加的信息。在重新打开该app之后,新增联系人才会显示出来。
最后附上源码(码云仓库):
链接: https://gitee.com/yangjy11/AndroidStudio/tree/master/WeiXinApplication/app/src/main.