SoFunction
Updated on 2025-04-09

Android enables call on and hang up

This article shares the specific codes for Android to enable call-on and hang up for your reference. The specific content is as follows

Key code: [PhoneUtils class]

package ; 
 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
 
import ; 
 
public class PhoneUtils { 
 static String TAG = "PhoneUtils"; 
 /**
   * Instantiate ITelephony from TelephonyManager and return
   */ 
 static public ITelephony getITelephony(TelephonyManager telMgr) 
   throws Exception { 
  Method getITelephonyMethod = ().getDeclaredMethod( 
    "getITelephony"); 
  (true);//Privatization functions can also be used  return (ITelephony) (telMgr); 
 } 
  
 //Automatic answer public static void autoAnswerPhone(Context c,TelephonyManager tm) { 
  try { 
   (TAG, "autoAnswerPhone"); 
   ITelephony itelephony = getITelephony(tm); 
   // (); 
   (); 
  } catch (Exception e) { 
   (); 
   try { 
    (TAG, "Used on Android 2.3 and 2.3 or above"); 
    Intent intent = new Intent(".MEDIA_BUTTON"); 
    KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, 
      KeyEvent.KEYCODE_HEADSETHOOK); 
    (".KEY_EVENT", keyEvent); 
    (intent, 
      ".CALL_PRIVILEGED"); 
    intent = new Intent(".MEDIA_BUTTON"); 
    keyEvent = new KeyEvent(KeyEvent.ACTION_UP, 
      KeyEvent.KEYCODE_HEADSETHOOK); 
    (".KEY_EVENT", keyEvent); 
    (intent, 
      ".CALL_PRIVILEGED"); 
    Intent localIntent1 = new Intent(Intent.ACTION_HEADSET_PLUG); 
    (Intent.FLAG_ACTIVITY_NO_HISTORY); 
    ("state", 1); 
    ("microphone", 1); 
    ("name", "Headset"); 
    (localIntent1, 
      ".CALL_PRIVILEGED"); 
    Intent localIntent2 = new Intent(Intent.ACTION_MEDIA_BUTTON); 
    KeyEvent localKeyEvent1 = new KeyEvent(KeyEvent.ACTION_DOWN, 
      KeyEvent.KEYCODE_HEADSETHOOK); 
    (".KEY_EVENT", 
      localKeyEvent1); 
    (localIntent2, 
      ".CALL_PRIVILEGED"); 
    Intent localIntent3 = new Intent(Intent.ACTION_MEDIA_BUTTON); 
    KeyEvent localKeyEvent2 = new KeyEvent(KeyEvent.ACTION_UP, 
      KeyEvent.KEYCODE_HEADSETHOOK); 
    (".KEY_EVENT", 
      localKeyEvent2); 
    (localIntent3, 
      ".CALL_PRIVILEGED"); 
    Intent localIntent4 = new Intent(Intent.ACTION_HEADSET_PLUG); 
    (Intent.FLAG_ACTIVITY_NO_HISTORY); 
    ("state", 0); 
    ("microphone", 1); 
    ("name", "Headset"); 
    (localIntent4, 
      ".CALL_PRIVILEGED"); 
   } catch (Exception e2) { 
    (); 
    Intent meidaButtonIntent = new Intent( 
      Intent.ACTION_MEDIA_BUTTON); 
    KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_UP, 
      KeyEvent.KEYCODE_HEADSETHOOK); 
    (Intent.EXTRA_KEY_EVENT, keyEvent); 
    (meidaButtonIntent, null); 
   } 
  } 
 } 
  
 //Original hang up public static void endPhone(Context c,TelephonyManager tm) { 
  try { 
   (TAG, "endPhone"); 
   ITelephony iTelephony; 
   Method getITelephonyMethod =  
     .getDeclaredMethod("getITelephony", (Class[]) null); 
   (true); 
   iTelephony = (ITelephony) (tm, 
     (Object[]) null); 
   // Hang up the phone   (); 
  } catch (Exception e) { 
   (); 
  } 
 } 
 
} 

Need to use:

package ; 
 /** 
 * Interface used to interact with the phone. Mostly this is used by the 
 * TelephonyManager class. A few places are still using this directly. 
 * Please clean them up if possible and use TelephonyManager instead. 
 * {@hide} 
 */ 
 
interface ITelephony {  
 /**  
 * End call or go to the Home screen 
 * @return whether it hung up  
 */  
 boolean endCall();  
  
 /**  
 * Answer the currently-ringing call.  
 *  
 * If there's already a current active call, that call will be  
 * automatically put on hold. If both lines are currently in use, the  
 * current active call will be ended.  
 *  
 * TODO: provide a flag to let the caller specify what policy to use  
 * if both lines are in use. (The current behavior is hardwired to  
 * "answer incoming, end ongoing", which is how the CALL button  
 * is specced to behave.)  
 *  
 * TODO: this should be a oneway call (especially since it's called  
 * directly from the key queue thread).  
 */  
 void answerRingingCall(); 
  
 /** 
  * Silence the ringer if an incoming call is currently ringing. 
  * (If vibrating, stop the vibrator also.) 
  * 
  * It's safe to call this if the ringer has already been silenced, or 
  * even if there's no incoming call. (If so, this method will do nothing.) 
  * 
  * TODO: this should be a oneway call too (see above). 
  *  (Actually *all* the methods here that return void can 
  *  probably be oneway.) 
  */ 
 void silenceRinger(); 
  
 /** 
  * Allow mobile data connections. 
  */ 
 boolean enableDataConnectivity(); 
 
 /** 
  * Disallow mobile data connections. 
  */ 
 boolean disableDataConnectivity(); 
 
 /** 
  * Report whether data connectivity is possible. 
  */ 
 boolean isDataConnectivityPossible(); 
} 

Listen to call broadcasts【】:

package ; 
 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
 
public class PhoneReceiver extends BroadcastReceiver { 
 String TAG = "PhoneReceiver"; 
 @Override 
 public void onReceive(Context context, Intent intent) { 
  TelephonyManager tm = (TelephonyManager) context 
    .getSystemService(Service.TELEPHONY_SERVICE); 
  switch (()) { 
  case TelephonyManager.CALL_STATE_OFFHOOK:// The call is called in and connected; the status first monitored when the call is called.   ("onCallStateChanged", "CALL_STATE_OFFHOOK"); 
   break; 
  case TelephonyManager.CALL_STATE_RINGING:// The call is in   ("onCallStateChanged", "CALL_STATE_RINGING"); 
   (context,tm); 
   break; 
  case TelephonyManager.CALL_STATE_IDLE:// The status that will be monitored whether it is a call or a call comes in.   ("onCallStateChanged", "CALL_STATE_IDLE"); 
   break; 
  } 
 } 
 
  
} 

Just add the code to hang up or call in the appropriate place of the above class.

Finally, don't forget to declare and register permissions in it.

<uses-permission android:name=".READ_PHONE_STATE" > 
</uses-permission> 
<uses-permission android:name=".PROCESS_OUTGOING_CALLS" > 
</uses-permission> 
<uses-permission android:name=".MODIFY_PHONE_STATE" /> 
<uses-permission android:name=".CALL_PHONE" /> 
<receiver android:name="" > 
 <intent-filter android:priority="2147483647" > 
 <action android:name=".PHONE_STATE" > 
 </action> 
 </intent-filter> 
</receiver> 

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.