"Mobile phone orderlies" Android automatically cut off incoming calls, if it is a mobile phone number to reply to a message

Source: Internet
Author: User

When your phone needs to change the new number, or when you travel abroad do not need to carry domestic mobile phone, your phone can be placed at home, when someone calls automatically hang up, if the other side is a mobile phone number (11 long first 1) will reply to a message.

Talk less, go directly to the code!

1, must have the following permissions:

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

2. In the project's Package Explorer, right-click on the new, package, input: Com.android.internal.telephony

3, in Com.android.internal.telephony right-click on the new, file, file name: Itelephony.aidl, enter the following:

Package Com.android.internal.telephony;interface itelephony{        boolean Endcall ();}

4. Add the following code to mainactivity:

    Private Itelephony itelephony = null;        Private Telephonymanager Itelemanager = null;    public void Inittelephony () {Itelemanager = (Telephonymanager) getsystemservice (Telephony_service);     Class <TelephonyManager> C = telephonymanager.class;     Method getitelephonymethod = null;     try {Getitelephonymethod = C.getdeclaredmethod ("Getitelephony", (class[]) null);     Getitelephonymethod.setaccessible (TRUE);     Itelephony = (itelephony) Getitelephonymethod.invoke (Itelemanager, (object[]) null);     } catch (IllegalArgumentException e) {e.printstacktrace ();     } catch (Exception e) {e.printstacktrace (); }} Phonestatelistener PSL = new Phonestatelistener () {@Override public void oncallstatechanged (int.    , String Incomingnumber) {//TODO auto-generated Method Stub super.oncallstatechanged (state, Incomingnumber); Switch (state) {case TELEPHONYMANAGER.CALL_STATE_RINGING:LOG.I ("DEBUG", "Uncall:" + incomingnumber);Message msg = new Message (); msg.what = 3618;msg.obj = Incomingnumber;handler.sendmessage (msg);   try {//when the phone is connected, hang up automatically.    Itelephony.endcall ();    } catch (RemoteException e) {//TODO auto-generated catch block E.printstacktrace ();    } break; }}}; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); s Etcontentview (R.layout.activity_main); Inittelephony (); Itelemanager.listen (PSL, Phonestatelistener.listen_call_ State);}        @Overrideprotected void OnDestroy () {//TODO auto-generated method Stubsuper.ondestroy (); Itelemanager.listen (null, 0);} Private Handler Handler = new Handler () {@Overridepublic void Handlemessage (Message msg) {//TODO auto-generated met        Hod stubsuper.handlemessage (msg); Myhandlemessage (msg);}        };     private void Myhandlemessage (Message msg) {if (Msg.what = = 3618) {String phonenumber = (string) msg.obj; if (phonenumber.length () = = && Phonenumber.charat (0) = = ' 1 ') {Smsmanager SMS = Smsmanager.getdefault (); String smsmsg = "Hello, the machine owner has updated the number, please call: 188 2318 3718." "Mobile phone orderlies" dedicated to provide you with quality service!     ";        Sms.sendtextmessage (PhoneNumber, NULL, smsmsg, NULL, NULL); }    }    }

At this point, the mobile phone orderlies automatic hanging machine and SMS notification of quality service declared complete!

"Mobile phone orderlies" Android automatically cut off incoming calls, if it is a mobile phone number to reply to a message

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.