Android 四大组件之ContentProvider 访问通讯录进行增删改查操作
Posted 路宇~
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 四大组件之ContentProvider 访问通讯录进行增删改查操作相关的知识,希望对你有一定的参考价值。
首先在清单文件和java文件中添加读取通讯录的权限
<!--允许应用程序写出到联系人-->
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
<!--允许应用程序读取到联系人-->
<uses-permission android:name="android.permission.READ_CONTACTS"/>
运行时权限
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_CONTACTS) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_CONTACTS, Manifest.permission.READ_CONTACTS}, 1);
}
}
之后activity_contact_resover.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ContactResoverActivity"
android:orientation="vertical"
>
<TextView
android:id="@+id/et_contact"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名"
/>
<EditText
android:id="@+id/et_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="电话"
/>
<Button
android:id="@+id/btn_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜索"
/>
<Button
android:id="@+id/btn_insert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="插入"
/>
<Button
android:id="@+id/btn_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除"
/>
<Button
android:id="@+id/btn_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="更新"
/>
<Button
android:id="@+id/btn_search_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查询所有"
/>
</LinearLayout>
接下里ContactResoverActivity文件,主要注释都已经给出了。
public class ContactResoverActivity extends AppCompatActivity implements View.OnClickListener {
private TextView et_contact; //显示查询的信息
private EditText et_username; //用户名
private EditText et_phone; //电话
private Button btn_search; //搜索
private Button btn_insert;//插入
private Button btn_delete; //删除
private Button btn_update; //更新
private Button btn_search_all; //查询所有
private String selections; //查询条件
private String[] selection_args; //查询参数
String[] query_all = new String[]{
ContactsContract.CommonDataKinds.Identity.RAW_CONTACT_ID, //用户id
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, //联系人姓名
ContactsContract.CommonDataKinds.Phone.NUMBER //联系人电话
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_resover);
et_contact = findViewById(R.id.et_contact);
et_username = findViewById(R.id.et_username);
et_phone = findViewById(R.id.et_phone);
btn_search = findViewById(R.id.btn_search);
btn_insert = findViewById(R.id.btn_insert);
btn_delete = findViewById(R.id.btn_delete);
btn_update = findViewById(R.id.btn_update);
btn_search_all = findViewById(R.id.btn_search_all);
btn_search_all.setOnClickListener(this);
btn_insert.setOnClickListener(this);
btn_delete.setOnClickListener(this);
btn_search.setOnClickListener(this);
btn_update.setOnClickListener(this);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_CONTACTS) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_CONTACTS, Manifest.permission.READ_CONTACTS}, 1);
}
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_insert:
String name = et_username.getText().toString();
String phone = et_phone.getText().toString();
ContentValues values = new ContentValues();
Uri uri = getContentResolver().insert(ContactsContract.RawContacts.CONTENT_URI, values);
long rawContentID = ContentUris.parseId(uri);
if (!name.equals("")) {
values.clear();
values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContentID);
values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
values.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, name);
getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
}
if (!phone.equals("")) {
values.clear();
values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContentID);
values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);
values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, phone);
getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
}
Toast.makeText(this, "插入成功", Toast.LENGTH_SHORT).show();
break;
case R.id.btn_search_all:
Cursor cursor1 = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, query_all, null, null, null);
printQueryResult(cursor1);
break;
case R.id.btn_delete:
String name1 = et_username.getText().toString();
if (!name1.equals("")) {
getContentResolver().delete(ContactsContract.RawContacts.CONTENT_URI, ContactsContract.Contacts.DISPLAY_NAME + "=?", new String[]{name1});
Toast.makeText(this, "删除成功!", Toast.LENGTH_SHORT).show();
}
break;
case R.id.btn_search:
String name_search = et_username.getText().toString();
Cursor cursor = getContentName(name_search);
printQueryResult(cursor);
break;
case R.id.btn_update:
String name_update = et_username.getText().toString();
String phone_update = et_phone.getText().toString();
Long rawContactId = 0L;
ContentResolver resolver = getContentResolver();
ContentValues values1 = new ContentValues();
values1.put(ContactsContract.CommonDataKinds.Phone.NUMBER, phone_update);
if (!name_update.equals("")) {
Cursor cursor2 = getContentName(name_update);
if (cursor2.moveToFirst()) {
rawContactId = cursor2.getLong(0);
}
resolver.update(ContactsContract.Data.CONTENT_URI, values1, "mimetype=? and raw_contact_id=?"
, new String[]{ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE, rawContactId + ""});
cursor2.close();
}
break;
}
}
private Cursor getContentName(String name_search) {
selections = ContactsContract.Contacts.DISPLAY_NAME + "=?";
selection_args = new String[]{name_search};
Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, query_all, selections, selection_args, null);
return cursor;
}
private void printQueryResult(Cursor cursor) {
if (cursor != null) {
et_contact.setText("");
while (cursor.moveToNext()) {
String ID = cursor.getString(0);
String stringName = cursor.getString(1);
String phone = cursor.getString(2);
et_contact.append("\\n联系人ID:" + ID + "\\n联系人姓名:" + stringName + "\\n联系人电话:" + phone);
}
}
cursor.close();
}
}
效果如图所示:
以上是关于Android 四大组件之ContentProvider 访问通讯录进行增删改查操作的主要内容,如果未能解决你的问题,请参考以下文章