//发短信填入号码
Uri uri = Uri.parse("smsto:" + phone);
Intent sendIntent = new Intent(Intent.ACTION_VIEW, uri);
sendIntent.putExtra("sms_body", subTitle);
context.startActivityForResult(sendIntent, 1002 );
//跳转到拨打电话界面
Uri uri = Uri.parse("tel:13876543210");
Intent intent = new Intent(Intent.ACTION_DIAL,uri);
startActivity(intent);
//直接拨号
Uri uri = Uri.parse("tel:13823214321");
Intent intent = new Intent(Intent.ACTION_CALL, uri);
startActivity(intent);
但是在6.0上面打电话需要动态请求权限,所以代码又改成这样:
private void testCallPhone() {
if (Build.VERSION.SDK_INT >= 23) {
//判断有没有拨打电话权限
if (PermissionChecker.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
//请求拨打电话权限
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, REQUEST_CODE);
} else {
callPhone("13823214321");
}
} else {
callPhone("13823214321");
}
}
/**
* 请求权限的回调方法
* @param requestCode 请求码
* @param permissions 请求的权限
* @param grantResults 权限的结果
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CODE && PermissionChecker.checkSelfPermission(context, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
ToastUtils.show(context, "授权成功");
callPhone("13823214321");
}
}
private void callPhone(String phoneNum) {
//直接拨号
Uri uri = Uri.parse("tel:" + phoneNum);
Intent intent = new Intent(Intent.ACTION_CALL, uri);
//此处不判断就会报错
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
startActivity(intent);
}
}
private final int REQUEST_CODE = 0x1001;
需要动态申请的权限还有
Permission Group | Permissions |
---|---|
android.permission-group.CALENDAR |
|
android.permission-group.CAMERA |
|
android.permission-group.CONTACTS |
|
android.permission-group.LOCATION |
|
android.permission-group.MICROPHONE |
|
android.permission-group.PHONE |
|
android.permission-group.SENSORS |
|
android.permission-group.SMS |
|
android.permission-group.STORAGE |
|
例如:一旦WRITE_EXTERNAL_STORAGE被授权了,app也有READ_EXTERNAL_STORAGE权限了。