Connect and disconnect the android app

Source: Internet
Author: User
Tags home screen

Package com. ebupt. phonerecorddemo. server; import java. lang. reflect. method; import android. content. context; import android. content. intent; import android. telephony. telephonyManager; import android. util. log; import android. view. keyEvent; import com. android. internal. telephony. ITelephony; public class PhoneUtils {static String TAG = "PhoneUtils";/*** instantiate ITelephony from TelephonyManager and return */static public ITelephony getITelephony (TelephonyManager telMgr) throws Exception {Method getITelephonyMethod = telMgr. getClass (). getDeclaredMethod ("getITelephony"); getITelephonyMethod. setAccessible (true); // The Private function can also use return (ITelephony) getITelephonyMethod. invoke (telMgr);} // automatically receives public static void autoAnswerPhone (Context c, TelephonyManager tm) {try {Log. I (TAG, "autoAnswerPhone"); ITelephony itelephony = getITelephony (tm); // itelephony. silenceRinger (); itelephony. answerRingingCall ();} catch (Exception e) {e. printStackTrace (); try {Log. e (TAG, "used for Android2.3 and later versions"); Intent intent = new Intent ("android. intent. action. MEDIA_BUTTON "); KeyEvent keyEvent = new KeyEvent (KeyEvent. ACTION_DOWN, KeyEvent. KEYCODE_HEADSETHOOK); intent. putExtra ("android. intent. extra. KEY_EVENT ", keyEvent); c. sendOrderedBroadcast (intent, "android. permission. CALL_PRIVILEGED "); intent = new Intent (" android. intent. action. MEDIA_BUTTON "); keyEvent = new KeyEvent (KeyEvent. ACTION_UP, KeyEvent. KEYCODE_HEADSETHOOK); intent. putExtra ("android. intent. extra. KEY_EVENT ", keyEvent); c. sendOrderedBroadcast (intent, "android. permission. CALL_PRIVILEGED "); Intent localIntent1 = new Intent (Intent. ACTION_HEADSET_PLUG); localIntent1.addFlags (Intent. FLAG_ACTIVITY_NO_HISTORY); localIntent1.putExtra ("state", 1); localIntent1.putExtra ("microphone", 1); localIntent1.putExtra ("name", "Headset"); c. sendOrderedBroadcast (localIntent1, "android. permission. CALL_PRIVILEGED "); Intent localIntent2 = new Intent (Intent. ACTION_MEDIA_BUTTON); KeyEvent localKeyEvent1 = new KeyEvent (KeyEvent. ACTION_DOWN, KeyEvent. KEYCODE_HEADSETHOOK); localIntent2.putExtra ("android. intent. extra. KEY_EVENT ", localKeyEvent1); c. sendOrderedBroadcast (localIntent2, "android. permission. CALL_PRIVILEGED "); Intent localIntent3 = new Intent (Intent. ACTION_MEDIA_BUTTON); KeyEvent localKeyEvent2 = new KeyEvent (KeyEvent. ACTION_UP, KeyEvent. KEYCODE_HEADSETHOOK); localIntent3.putExtra ("android. intent. extra. KEY_EVENT ", localKeyEvent2); c. sendOrderedBroadcast (localIntent3, "android. permission. CALL_PRIVILEGED "); Intent localIntent4 = new Intent (Intent. ACTION_HEADSET_PLUG); localIntent4.addFlags (Intent. FLAG_ACTIVITY_NO_HISTORY); localIntent4.putExtra ("state", 0); localIntent4.putExtra ("microphone", 1); localIntent4.putExtra ("name", "Headset"); c. sendOrderedBroadcast (localIntent4, "android. permission. CALL_PRIVILEGED ");} catch (Exception e2) {e2.printStackTrace (); Intent meidaButtonIntent = new Intent (Intent. ACTION_MEDIA_BUTTON); KeyEvent keyEvent = new KeyEvent (KeyEvent. ACTION_UP, KeyEvent. KEYCODE_HEADSETHOOK); meidaButtonIntent. putExtra (Intent. EXTRA_KEY_EVENT, keyEvent); c. sendOrderedBroadcast (meidaButtonIntent, null) ;}}// automatically hangs up public static void endPhone (Context c, TelephonyManager tm) {try {Log. I (TAG, "endPhone"); ITelephony iTelephony; Method getITelephonyMethod = TelephonyManager. class. getDeclaredMethod ("getITelephony", (Class []) null); getITelephonyMethod. setAccessible (true); iTelephony = (ITelephony) getITelephonyMethod. invoke (tm, (Object []) null); // call iTelephony. endCall ();} catch (Exception e) {e. printStackTrace ();}}}
package com.android.internal.telephony;   /**    * 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();} 
Package com. ebupt. phonerecorddemo. server; import android. app. service; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. telephony. telephonyManager; import android. util. log; public class PhoneReceiver extends BroadcastReceiver {String TAG = "PhoneReceiver"; @ Overridepublic void onReceive (Context context, Intent intent) {TelephonyManager t M = (TelephonyManager) context. getSystemService (Service. TELEPHONY_SERVICE); switch (tm. getCallState () {case TelephonyManager. CALL_STATE_OFFHOOK: // call in and connection status; the status that is first monitored when the call is made. Log. I ("onCallStateChanged", "CALL_STATE_OFFHOOK"); break; case TelephonyManager. CALL_STATE_RINGING: // call in status Log. I ("onCallStateChanged", "CALL_STATE_RINGING"); PhoneUtils. autoAnswerPhone (context, tm); break; case TelephonyManager. CALL_STATE_IDLE: // the status that will be monitored whether the call is made or the call is made. Log. I ("onCallStateChanged", "CALL_STATE_IDLE"); break ;}}}

 

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.