SoFunction
Updated on 2025-04-23

Sample code for Android implementing automatic hangup phone function

When developing Android applications, you sometimes encounter the need to automatically hang up the phone. For example, in certain specific situations (such as when the user is using an important feature), the phone does not want the user to interrupt the operation. This article will introduce how to automatically hang up the phone in Android.

1. Authorization application

To automatically hang up the phone, you first need to declare the necessary permissions in the file:

<uses-permission android:name=".READ_PHONE_STATE" />
<uses-permission android:name=".CALL_PHONE" />

Starting with Android 6.0 (API level 23), in addition to declaring permissions in, you also need to request these permissions at runtime. Here is a simple example showing how to request permissions at runtime:

if ((this, .READ_PHONE_STATE)
    != PackageManager.PERMISSION_GRANTED || (this, .CALL_PHONE)
    != PackageManager.PERMISSION_GRANTED) {
    (this,
        new String[]{.READ_PHONE_STATE, .CALL_PHONE},
        MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
}

2. Monitor the phone status

In order to perform a hang-up operation when a call is in incoming, we need to monitor the status changes of the phone. This can be done by registering a PhoneStateListener. Here is an example of monitoring the status of the phone:

TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
 
PhoneStateListener phoneStateListener = new PhoneStateListener() {
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        (state, incomingNumber);
        if (state == TelephonyManager.CALL_STATE_RINGING) {
            // When the phone calls in            ("PhoneCall", "Incoming number: " + incomingNumber);
            // Here you can call the method of hanging up the phone            hangUpCall();
        }
    }
};
 
(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);

3. Hang up the phone

In Android, hanging up the phone directly is not a publicly supported operation, but can be achieved by sending broadcasts or using reflection. Here is an example of using the reflection method to hang up the phone:

private void hangUpCall() {
    try {
        // Get TelecomManager        TelecomManager telecomManager = (TelecomManager) getSystemService(Context.TELECOM_SERVICE);
        if (telecomManager != null) {
            Method method = ().getDeclaredMethod("endCall");
            (telecomManager);
        }
    } catch (Exception e) {
        ();
    }
}

Note that this approach relies on the Android internal API and may behave inconsistently on different Android versions or devices. Additionally, starting with Android 8.0 (API level 26), the TelecomManager class provides an official endCall() method, but this method requires MODIFY_PHONE_STATE permission, which is a system-level permission that is not available to ordinary applications.

4. Testing and debugging

After implementing the above features, be sure to test on different versions of Android devices to ensure the stability and compatibility of the features. Especially when handling phone status and hang-up operations, pay attention to exception handling to avoid application crashes due to permission problems or other reasons.

Since user privacy and security involve, this function should be used with caution and ensure compliance with relevant laws and regulations. Implementing the function of automatically hang up calls in Android requires the use of TelephonyManager and ITelephony interfaces. This feature is usually used to develop specific application scenarios, such as anti-harassment software or automated testing tools. It should be noted that this operation may involve user privacy and security issues, so it needs to be handled with caution in actual applications and ensure that the user is explicitly authorized.

5. Method supplement

Here is a simple example code showing how to automatically hang up the phone when it is incoming. Please note that this example is for learning and reference only and requires sufficient testing and legality verification before actual deployment.

1. Add permissions

First, add the necessary permissions to the file:

<uses-permission android:name=".READ_PHONE_STATE" />
<uses-permission android:name=".CALL_PHONE" />
<uses-permission android:name=".PROCESS_OUTGOING_CALLS" />

2. Create a broadcast receiver

Create a broadcast receiver to listen for incoming call events and perform a hangup operation on incoming calls.

import ;
import ;
import ;
import ;
import ;
 
public class CallReceiver extends BroadcastReceiver {
 
    @Override
    public void onReceive(Context context, Intent intent) {
        if (().equals(".PHONE_STATE")) {
            String state = (TelephonyManager.EXTRA_STATE);
            if ((TelephonyManager.EXTRA_STATE_RINGING)) {
                // Get the call number                String incomingNumber = (TelephonyManager.EXTRA_INCOMING_NUMBER);
                // Hang up the phone                endCall(context);
            }
        }
    }
 
    private void endCall(Context context) {
        try {
            // Get TelephonyManager            TelephonyManager telephonyManager = (TelephonyManager) (Context.TELEPHONY_SERVICE);
            // Get ITelephony interface            Class&lt;?&gt; telephonyClass = (().getName());
            Method getITelephonyMethod = ("getITelephony");
            (true);
            Object iTelephony = (telephonyManager);
            // Call the endCall method            Class&lt;?&gt; iTelephonyClass = ("");
            Method endCallMethod = ("endCall");
            (iTelephony);
        } catch (Exception e) {
            ();
        }
    }
}

3. Register a broadcast receiver

Register a broadcast receiver in ​​​​:

<receiver android:name=".CallReceiver">
    <intent-filter>
        <action android:name=".PHONE_STATE" />
    </intent-filter>
</receiver>

4. Dynamic request permissions

Starting with Android 6.0 (API level 23), you need to request dangerous permissions at runtime. You can request these permissions in the main activity:

import ;
import ;
import ;
import ;
import ;
import ;
import ;
 
public class MainActivity extends AppCompatActivity {
 
    private static final int REQUEST_PHONE_CALL = 1;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        (savedInstanceState);
        setContentView(.activity_main);
 
        if ((this, .READ_PHONE_STATE)
                != PackageManager.PERMISSION_GRANTED || (this, .CALL_PHONE)
                != PackageManager.PERMISSION_GRANTED) {
            (this,
                    new String[]{.READ_PHONE_STATE, .CALL_PHONE},
                    REQUEST_PHONE_CALL);
        }
    }
 
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        (requestCode, permissions, grantResults);
        if (requestCode == REQUEST_PHONE_CALL) {
            if ( &gt; 0 &amp;&amp; grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permissions have been granted            } else {
                // Permission denied            }
        }
    }
}

Things to note

  • Security: Automatic hang-up may affect the user's normal use, ensuring that the user knows and agrees to this feature.
  • Compatibility: Different Android versions and devices may have different implementation details, and it is recommended to test them on multiple devices.
  • Legal compliance: Make sure your application complies with local laws and regulations, especially when processing user call data.

Automatically hang up the phone in Android

Implementing the ability to automatically hang up a phone in Android usually involves using TelephonyManager and BroadcastReceiver to listen for phone status and perform hangup under certain conditions. Here is a detailed implementation step and sample code:

1. Add permissions

First, add the necessary permissions to your file:

<uses-permission android:name=".READ_PHONE_STATE" />
<uses-permission android:name=".CALL_PHONE" />
<uses-permission android:name=".PROCESS_OUTGOING_CALLS" />

2. Create a BroadcastReceiver

Create a BroadcastReceiver to listen for changes in phone status.

import ;
import ;
import ;
import ;
 
public class PhoneStateReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = ();
        if ((TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
            String state = (TelephonyManager.EXTRA_STATE);
            if ((TelephonyManager.EXTRA_STATE_RINGING)) {
                // The phone is ringing                String incomingNumber = (TelephonyManager.EXTRA_INCOMING_NUMBER);
                // Here you can add logic to decide whether to hang up the phone                if (shouldHangUp(incomingNumber)) {
                    hangUp(context);
                }
            }
        }
    }
 
    private boolean shouldHangUp(String incomingNumber) {
        //Judge whether to hang up the phone according to the required conditions        return ("1234567890"); // Example: Hang up the phone from 1234567890    }
 
    private void hangUp(Context context) {
        try {
            // Get TelephonyManager            TelephonyManager telephonyManager = (TelephonyManager) (Context.TELEPHONY_SERVICE);
            Class&lt;?&gt; telephonyClass = (().getName());
            Method getITelephonyMethod = ("getITelephony");
            (true);
            Object iTelephony = (telephonyManager);
 
            // Call the hangup method            Class&lt;?&gt; iTelephonyClass = (().getName());
            Method endCallMethod = ("endCall");
            (iTelephony);
        } catch (Exception e) {
            ();
        }
    }
}

3. Register BroadcastReceiver

Register the BroadcastReceiver in the ​​​​so the system can call it when the phone state changes.

<receiver android:name=".PhoneStateReceiver">
    <intent-filter>
        <action android:name=".PHONE_STATE" />
    </intent-filter>
</receiver>

4. Dynamic registration (optional)

If you want to register BroadcastReceiver dynamically at runtime, you can do the following in your Activity or Service:

import ;
import ;
import ;
 
public class MainActivity extends AppCompatActivity {
    private PhoneStateReceiver phoneStateReceiver;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        (savedInstanceState);
        setContentView(.activity_main);
 
        phoneStateReceiver = new PhoneStateReceiver();
        IntentFilter filter = new IntentFilter(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
        registerReceiver(phoneStateReceiver, filter);
    }
 
    @Override
    protected void onDestroy() {
        ();
        unregisterReceiver(phoneStateReceiver);
    }
}

5. Things to note

Permissions Issue: Starting with Android 6.0 (API level 23), you need to request dangerous permissions at runtime.

Security: The operation of hanging up the phone may be restricted by the system, especially in the latest Android versions. Make sure your app has reasonable reasons to perform such actions and comply with Google Play's policies.

Compatibility: Different devices and ROMs may have different implementation details, so testing on multiple devices is recommended.

Through the above steps, you can automatically hang up the phone in Android applications.

This is the article about the example code of Android implementing the automatic hang-up call function. For more information about Android's automatic hang-up call, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!