Explanation of automatic answering and hanging up for Android phones

Source: Internet
Author: User

Explanation of automatic answering and hanging up for Android phones

1. Call up through aidl and reflection

There are three steps:
(1) ITelephony. aidl. You must create a new com. android. internal. telephony package and put it into ITelephony. aidl file (after construction, there is ITelephony under gen. java file, which is the interface generated by aidl). The file content is as follows:
Package com. android. internal. telephony;
Interface ITelephony {
Boolean endCall ();
Void answerRingingCall ();
}
(2) Add the following method to the required class. The Code is as follows (obtain the telephone interface instance through reflection)

/**
* @ Param context
* @ Return
*/
Private static ITelephony getITelephony (Context context ){
TelephonyManager mTelephonyManager = (TelephonyManager) context
. GetSystemService (TELEPHONY_SERVICE );
Class C = TelephonyManager. class;
Method getITelephonyMethod = null;
Try {
GetITelephonyMethod = c. getDeclaredMethod ("getITelephony ",
(Class []) null); // method for obtaining the Declaration
GetITelephonyMethod. setAccessible (true );
} Catch (SecurityException e ){
E. printStackTrace ();
} Catch (NoSuchMethodException e ){
E. printStackTrace ();
}

Try {
ITelephony iTelephony = (ITelephony) getITelephonyMethod. invoke (
MTelephonyManager, (Object []) null); // get the instance
Return iTelephony;
} Catch (Exception e ){
E. printStackTrace ();
}
Return iTelephony;
}

(3) call this instance when calling, and then call this endCall () method.

MTelephonyManager = (TelephonyManager) this
. GetSystemService (TELEPHONY_SERVICE );
MTelephonyManager. listen (phoneStateListener,
PhoneStateListener. LISTEN_CALL_STATE );

// Telephone instance
PhoneStateListener phoneStateListener = new PhoneStateListener (){

@ Override
Public void onCallStateChanged (int state, String incomingNumber ){

Switch (state ){
Case TelephonyManager. CALL_STATE_RINGING:
ITelephony = getITelephony (getApplicationContext (); // obtain the call interface
If (iTelephony! = Null ){
Try {
ITelephony. endCall (); // end the call.
Toast. makeText (getApplicationContext (),
"EndCall" + incomingNumber + "successful! ", 3000). show ();
} Catch (RemoteException e ){
E. printStackTrace ();
}
}
Break;
Default:
Break;
}
}

};


Aidl: http://download.csdn.net/detail/ab6326795/7993671


The above method is applicable to versions earlier than 2.3, and more than 2.3 cannot be used.


2. Use the broadcast notification system to answer and stop

Because Android2.3 and above have added restrictions on permissionandroid. permission. MODIFY_PHONE_STATE, the method of calling ITelephone through reflection mechanism before 2.3 is no longer applicable.
2.3 Implementation:
Public synchronized void answerRingingCall (){

The PhoneGlobals. java API allows you to receive and process notifications such as headset insertion and multimedia buttons. No special content is found. I personally think that if the system receives this broadcast, it should be able to answer or stop up.

// 2.3 or above, execute the following code to automatically answer Intent mintent = new Intent (Intent. ACTION_MEDIA_BUTTON); // press the volume KeyEvent keyEvent = new KeyEvent (KeyEvent. ACTION_DOWN, KeyEvent. KEYCODE_HEADSETHOOK); mintent. putExtra ("android. intent. extra. KEY_EVENT ", keyEvent); // call permission allows the program to make a call, replacing the mContext of the system's dialer interface. sendOrderedBroadcast (mintent, "android. permission. CALL_PRIVILEGED "); mintent = new Intent (Intent. ACTION_MEDIA_BUTTON); keyEvent = new KeyEvent (KeyEvent. ACTION_UP, KeyEvent. KEYCODE_HEADSETHOOK); mintent. putExtra ("android. intent. extra. KEY_EVENT ", keyEvent); mContext. sendOrderedBroadcast (mintent, "android. permission. CALL_PRIVILEGED ");


Both require Permissions

    
       
          
      
  


Two methods can be used to automatically answer and stop the phone call.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.