如何在 Android 中为 Marshmallow 创建一个 phone 呼叫按钮
How to make a phone call button in Android for Marshmallow
我正在尝试在 Android 中创建一个呼叫按钮。我创建了一个,它适用于所有类型的 Android 设备,但不适用于 Marshmallow。我不知道为什么。如何为 android Marshmallow 创建调用按钮?
这是我用于调用按钮的代码:
public void button(View v) {
String number = "12354656";
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + number));
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
return;
}
startActivity(intent);
}
此问题可能与 Android 6.0
中引入的 Runtime Permissions
有关。
你可以试试这个:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE},REQUEST_PHONE_CALL);
}
else
{
startActivity(intent);
}
您需要在 phone 设置中为您的应用启用权限。查看设置中的权限
你一定要试试这个...
输入您的 android 清单
<uses-permission android:name="android.permission.CALL_PHONE" />
获取 make phone 调用的运行时权限(Android API 版本 23 及以上)
private static final int REQUEST_PERMISSION_CALL = 111;
if (ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.CALL_PHONE)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
Manifest.permission.CALL_PHONE)) {
ActivityCompat.requestPermissions(this, getString(R.string.app_need_call_permission),
REQUEST_PERMISSION_CALL, Manifest.permission.CALL_PHONE);
} else {
ActivityCompat.requestPermissions(this, getString(R.string.app_need_call_permission),
REQUEST_PERMISSION_CALL, Manifest.permission.CALL_PHONE);
}
}
您将在
上获得许可响应
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_PERMISSION_CALL) {
// put your code here to make call
}
}
然后您必须拨打 phone 电话
Uri number = Uri.parse("tel:123456789");
Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
startActivity(callIntent);
了解更多信息https://developer.android.com/training/permissions/requesting.html
希望对您有所帮助。
我正在尝试在 Android 中创建一个呼叫按钮。我创建了一个,它适用于所有类型的 Android 设备,但不适用于 Marshmallow。我不知道为什么。如何为 android Marshmallow 创建调用按钮?
这是我用于调用按钮的代码:
public void button(View v) {
String number = "12354656";
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + number));
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
return;
}
startActivity(intent);
}
此问题可能与 Android 6.0
中引入的 Runtime Permissions
有关。
你可以试试这个:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE},REQUEST_PHONE_CALL);
}
else
{
startActivity(intent);
}
您需要在 phone 设置中为您的应用启用权限。查看设置中的权限
你一定要试试这个...
输入您的 android 清单
<uses-permission android:name="android.permission.CALL_PHONE" />
获取 make phone 调用的运行时权限(Android API 版本 23 及以上)
private static final int REQUEST_PERMISSION_CALL = 111;
if (ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.CALL_PHONE)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
Manifest.permission.CALL_PHONE)) {
ActivityCompat.requestPermissions(this, getString(R.string.app_need_call_permission),
REQUEST_PERMISSION_CALL, Manifest.permission.CALL_PHONE);
} else {
ActivityCompat.requestPermissions(this, getString(R.string.app_need_call_permission),
REQUEST_PERMISSION_CALL, Manifest.permission.CALL_PHONE);
}
}
您将在
上获得许可响应@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_PERMISSION_CALL) {
// put your code here to make call
}
}
然后您必须拨打 phone 电话
Uri number = Uri.parse("tel:123456789");
Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
startActivity(callIntent);
了解更多信息https://developer.android.com/training/permissions/requesting.html
希望对您有所帮助。