我正在尝试在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);
}
答案 0 :(得分:1)
该问题可能与Runtime Permissions
中引入的Android 6.0
有关。
你可以试试这个:
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);
}
答案 1 :(得分:1)
您需要在手机设置中为您的应用启用权限。查看设置中的权限
答案 2 :(得分:0)
你必须尝试这个......
放入你的Android清单
<uses-permission android:name="android.permission.CALL_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
}
}
然后你必须拨打电话
Uri number = Uri.parse("tel:123456789");
Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
startActivity(callIntent);
了解更多信息https://developer.android.com/training/permissions/requesting.html
希望这会对你有所帮助。