简体   繁体   中英

Android Studio making a phone call in API 23

Hello I wanting to make a simple button click which allows the user to call a specific number when pressed. Before API 23 I code simply add the permission to the manifest and away we go but now I have to add this self check system. The problem I having with the line

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {

As (this) has a red underline telling me it's the wrong 1st argument type. Being that this is my first time with this self check problem, how would I go about fixing the error?


call = (Button) findViewById(R.id.btnCall);

    call.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:123"));

            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {

                return;
            }
            startActivity(callIntent);




        }
    });

Thank you

Eternal problem :)

Change this to getBaseContext() or to <YourActivityClass>.this

if (ActivityCompat.checkSelfPermission(getBaseContext(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
    return;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM