How to make a phone call button in Android for Marshmallow

只愿长相守 提交于 2019-12-11 06:28:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!