具体要求:
? ? ? ? (1)添加动态联系人的权限。
? ? ? ? (2)创建Activity和布局文件,添加输入框和按钮等控件。
? ? ? ? (3)完成添加联系人的功能。
activity_main.xml代码
<!-- activity_main.xml -->
<RelativeLayout 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=".MainActivity">
<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="姓名" />
<EditText
android:id="@+id/editTextPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/editTextName"
android:layout_marginTop="16dp"
android:hint="电话" />
<Button
android:id="@+id/buttonAddContact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editTextPhone"
android:layout_marginTop="16dp"
android:text="添加" />
</RelativeLayout>
MainActivity.java代码
package com.qcxy.lesson6;
import android.content.ContentProviderOperation;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private EditText editTextName, editTextPhone;
private Button buttonAddContact;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextName = findViewById(R.id.editTextName);
editTextPhone = findViewById(R.id.editTextPhone);
buttonAddContact = findViewById(R.id.buttonAddContact);
buttonAddContact.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addContact();
}
});
}
private void addContact() {
String name = editTextName.getText().toString().trim();
String phone = editTextPhone.getText().toString().trim();
if (name.isEmpty() || phone.isEmpty()) {
Toast.makeText(this, "Name and Phone cannot be empty", Toast.LENGTH_SHORT).show();
return;
}
ContentResolver contentResolver = getContentResolver();
ArrayList<ContentProviderOperation> operations = new ArrayList<>();
// 添加姓名
operations.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
.build());
operations.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name)
.build());
// 添加电话号码
operations.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phone)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
.build());
try {
contentResolver.applyBatch(ContactsContract.AUTHORITY, operations);
Toast.makeText(this, "Contact added successfully", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "Error adding contact", Toast.LENGTH_SHORT).show();
}
}
}
记得在mainfest.xml中添加下面的代码,负责会报错哦
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>