Android phone-related operations-call, answer, and call

Source: Internet
Author: User

Because android2.3 and above have added restrictions on permission Android. Permission. modify_phone_state, the method of calling itelephone through the reflection mechanism before 2.3 is no longer applicable, so you can use the counterfeit broadcast method to answer the phone.


Package COM. demo. launcher; import Java. lang. reflect. invocationtargetexception; import Java. lang. reflect. method; import android. content. context; import android. content. intent; import android. media. audiomanager; import android.net. uri; import android. telephony. telephonymanager; import android. text. textutils; import android. view. keyevent; public class phoneutils {public static string tag = phoneutils. class. gets Implename (); public static void endcall (context) {try {object telephonyobject = gettelephonyobject (context); If (null! = Telephonyobject) {class telephonyclass = telephonyobject. getclass (); Method endcallmethod = telephonyclass. getmethod ("endcall"); endcallmethod. setaccessible (true); endcallmethod. invoke (telephonyobject) ;}} catch (securityexception e) {e. printstacktrace ();} catch (nosuchmethodexception e) {e. printstacktrace ();} catch (illegalargumentexception e) {e. printstacktrace ();} catch (illegalaccessexception E) {E. printstacktrace ();} catch (invocationtargetexception e) {e. printstacktrace () ;}} private static object gettelephonyobject (context) {object telephonyobject = NULL; try {// initialize itelephonytelephonymanager telephonymanager = (telephonymanager) context. getsystemservice (context. telephony_service); // will be used to invoke hidden methods with reflection // get the current object implementing itelep Hony interfaceclass telmanager = telephonymanager. getclass (); Method getitelephony = telmanager. getdeclaredmethod ("getitelephony"); getitelephony. setaccessible (true); telephonyobject = getitelephony. invoke (telephonymanager);} catch (securityexception e) {e. printstacktrace ();} catch (nosuchmethodexception e) {e. printstacktrace ();} catch (illegalargumentexception e) {e. printstacktrace ();} catch (illega Laccessexception e) {e. printstacktrace ();} catch (invocationtargetexception e) {e. printstacktrace ();} return telephonyobject;}/*** call the method through reflection to answer the call. This method is only valid on the Android 2.3 system. * @ Param context */Private Static void answerringingcallwithreflect (context) {try {object telephonyobject = gettelephonyobject (context); If (null! = Telephonyobject) {class telephonyclass = telephonyobject. getclass (); Method endcallmethod = telephonyclass. getmethod ("answerringingcall"); endcallmethod. setaccessible (true); endcallmethod. invoke (telephonyobject); // itelephony = (itelephony) telephonyobject; // itelephony. answerringingcall () ;}} catch (securityexception e) {e. printstacktrace ();} catch (illegalargumentexception e) {e. prin Tstacktrace ();} catch (illegalaccessexception e) {e. printstacktrace ();} catch (invocationtargetexception e) {e. printstacktrace ();} catch (nosuchmethodexception e) {e. printstacktrace () ;}}/*** counterfeit a wired headset and press the listen key to broadcast it so that the system starts to answer the call. * @ Param context */Private Static void answerringingcallwithbroadcast (context) {audiomanager localaudiomanager = (audiomanager) context. getsystemservice (context. audio_service); // determines whether the headset is configured with Boolean iswiredheadseton = localaudiomanager. iswiredheadseton (); If (! Iswiredheadseton) {intent headsetpluggedintent = new intent (intent. action_headset_plug); headsetpluggedintent. putextra ("State", 1); headsetpluggedintent. putextra ("Microphone", 0); headsetpluggedintent. putextra ("name", ""); context. sendbroadcast (headsetpluggedintent); intent meidabuttonintent = new intent (intent. action_media_button); keyevent = new keyevent (keyevent. action_up, keyevent. keycode_he Adsethook); meidabuttonintent. putextra (intent. extra_key_event, keyevent); context. sendorderedbroadcast (meidabuttonintent, null); intent headsetunpluggedintent = new intent (intent. action_headset_plug); headsetunpluggedintent. putextra ("State", 0); headsetunpluggedintent. putextra ("Microphone", 0); headsetunpluggedintent. putextra ("name", ""); context. sendbroadcast (headsetunpluggedintent);} else {intent meidab Uttonintent = new intent (intent. action_media_button); keyevent = new keyevent (keyevent. action_up, keyevent. keycode_headsethook); meidabuttonintent. putextra (intent. extra_key_event, keyevent); context. sendorderedbroadcast (meidabuttonintent, null) ;}/ ***** @ Param context */public static void answerringingcall (context) {If (baseactivity. isgingerbreadorlater () {answerringingcallwithbroad Cast (context);} else {answerringingcallwithreflect (context);}/***** @ Param context * @ Param phonenumber */public static void callphone (context, string phonenumber) {If (! Textutils. isempty (phonenumber) {try {intent callintent = new intent (intent. action_call, Uri. parse ("Tel:" + phonenumber); context. startactivity (callintent);} catch (exception e) {e. printstacktrace () ;}}/ ***** @ Param context * @ Param phonenumber */public static void dialphone (context, string phonenumber) {If (! Textutils. isempty (phonenumber) {try {intent callintent = new intent (intent. action_dial, Uri. parse ("Tel:" + phonenumber); context. startactivity (callintent);} catch (exception e) {e. printstacktrace ();}}}}
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.