Android studio中写一个用contentResolver()方法实现读取通讯录并功能实现发短信和打电话的代码
时间: 2024-03-17 18:44:10 浏览: 178
AndroidContact.zip
以下是一个使用contentResolver()方法实现读取通讯录并实现发短信和打电话功能的示例代码:
```java
public class MainActivity extends AppCompatActivity {
private static final int READ_CONTACTS_PERMISSIONS_REQUEST = 1;
private static final int REQUEST_SEND_SMS = 2;
private static final int REQUEST_CALL_PHONE = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_CONTACTS},
READ_CONTACTS_PERMISSIONS_REQUEST);
} else {
readContacts();
}
}
private void readContacts() {
String[] projection = new String[]{ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER};
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " ASC";
Cursor cursor = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection,
null,
null,
sortOrder);
if (cursor != null && cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String name = cursor.getString(0);
String phone = cursor.getString(1);
Log.d("Contacts", name + " - " + phone);
}
cursor.close();
}
}
public void sendMessage(String phoneNumber, String message) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.SEND_SMS},
REQUEST_SEND_SMS);
} else {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, message, null, null);
Toast.makeText(this, "Message sent successfully", Toast.LENGTH_SHORT).show();
}
}
public void callPhoneNumber(String phoneNumber) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CALL_PHONE},
REQUEST_CALL_PHONE);
} else {
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber));
startActivity(callIntent);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == READ_CONTACTS_PERMISSIONS_REQUEST) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
readContacts();
}
} else if (requestCode == REQUEST_SEND_SMS) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission granted, send SMS
sendMessage("1234567890", "Hello, world!");
}
} else if (requestCode == REQUEST_CALL_PHONE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission granted, call phone
callPhoneNumber("1234567890");
}
}
}
}
```
在这个示例代码中,`readContacts()`方法使用`contentResolver()`方法读取通讯录,并将结果输出到Logcat中。`sendMessage()`方法使用`SmsManager`类发送短信。`callPhoneNumber()`方法使用`Intent.ACTION_CALL`和`Uri.parse()`来拨打电话。在`onRequestPermissionsResult()`方法中,我们检查用户是否授予了所需的权限,并在授予权限后执行相应的操作。请注意,这些权限请求的响应代码已在示例代码中定义。
阅读全文