Motamred Motamred - 4 years ago 129
Android Question

android request runtime permission to call action

i have code for call action and i need best way to declare run time permission ive tried many codes but i always get error

here is my basic code any suggestion for make it work with runtime permission
thanks in advance

public class MainActivity extends Activity {

private Button button;
private EditText etPhoneno;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

button = (Button) findViewById(R.id.buttonCall);
etPhoneno = (EditText) findViewById(R.id.editText1);
// add button listener
button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
String phnum = etPhoneno.getText().toString();
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + phnum));
startActivity(callIntent);
}
});
}


}

Answer Source

try this code in your onClick()

if(isPermissionGranted()){
 call_action();
}

Now for call create a separate method:

public void call_action(){
    String phnum = etPhoneno.getText().toString();
                    Intent callIntent = new Intent(Intent.ACTION_CALL);
                    callIntent.setData(Uri.parse("tel:" + phnum));
                    startActivity(callIntent);
        }

Add this two function for runtime permission check:

    public  boolean isPermissionGranted() {
    if (Build.VERSION.SDK_INT >= 23) {
        if (checkSelfPermission(android.Manifest.permission.CALL_PHONE)
                == PackageManager.PERMISSION_GRANTED) {
            Log.v("TAG","Permission is granted");
            return true;
        } else {

            Log.v("TAG","Permission is revoked");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, 1);
            return false;
        }
    }
    else { //permission is automatically granted on sdk<23 upon installation
        Log.v("TAG","Permission is granted");
        return true;
    }
}


 @Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {

        case 1: {

            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(getApplicationContext(), "Permission granted", Toast.LENGTH_SHORT).show();
                call_action();
            } else {
                Toast.makeText(getApplicationContext(), "Permission denied", Toast.LENGTH_SHORT).show();
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
 }

Also make sure u add this into manifest:

<uses-permission android:name="android.permission.CALL_PHONE" />
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download