[Android Studio] Call and Hang Up on Android Programmatically

Steps to make a phone call and hang up on android the phone programmatically.


1. Add permission to make a phone call.
<uses-permission android:name="android.permission.CALL_PHONE" />

2. A function to make a call to the phone_no.
void PhoneCall(String phone_no){

    Intent intent = new Intent(Intent.ACTION_CALL);
    LogWrapper.d("phonecall",phone_no);
    intent.setData(Uri.parse("tel:"+phone_no));
    startActivity(intent);
}



3. In order to hang up the call programmatically, the device has to be rooted.
Such action of modifying phone state is a system permission,
which is not available to 3rd party applications.
Use SuperSu to execute the command to hang up as a root.

void EndCall(){
    try {
        Process root = Runtime.getRuntime().exec("su && input keyevent 6");
    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(this, "failed to end the call", Toast.LENGTH_SHORT).show();
    }
}





Comments