问题
I'm trying to create a calling button in Android. I created one and it works in all kind of Android device but it's not working on Marshmallow. I don't know why. How to create a calling button for android Marshmallow?
This is the code I used for the calling button :
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);
}
回答1:
You'll need to enable permission for your app in the phone settings. Look under permissions in settings
回答2:
The issue is probably related to Runtime Permissions
introduced in Android 6.0
.
You can try this:
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);
}
回答3:
You have to try this...
put in your android manifest
<uses-permission android:name="android.permission.CALL_PHONE" />
get runtime permission for make phone call(Android API version 23 and above)
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);
}
}
you will get permission response on
@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
}
}
then you have to make your phone call
Uri number = Uri.parse("tel:123456789");
Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
startActivity(callIntent);
for more info https://developer.android.com/training/permissions/requesting.html
Hope this will help you.
来源:https://stackoverflow.com/questions/45693593/how-to-make-a-phone-call-button-in-android-for-marshmallow