我正在尝试在Android中创建一个呼叫按钮。我创建了一个,它可以在所有类型的Android设备上工作,但它不能在棉花糖上工作。我也不知道原因。如何为android棉花糖创建呼叫按钮?
这是我用于呼叫按钮的代码:
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);
}
发布于 2017-08-15 22:02:26
你需要在手机设置中为你的应用程序启用权限。查看设置中的权限
发布于 2017-08-15 21:23:11
该问题可能与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);
}
发布于 2017-08-15 22:41:51
你得试试这个..。
放入你的安卓清单
<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
希望这能对你有所帮助。
https://stackoverflow.com/questions/45693593
复制相似问题