Interception and judgment of incoming call information

Source: Internet
Author: User

How do I intercept a call and automatically hang up when certain numbers are detected?
Use reflection technology to access the internal features of the Android SDK to hang up the phone
1. The OnReceive () method of the broadcast receiver class (INCALLRECEIVER) that intercepts incoming calls

1  Public voidOnReceive (FinalContext context,intent Intent) {2  //Get a phone management service to get phone status3Telephonymanager tm=(Telephonymanager) Context.getsystemservice (service.telephony_service);4  //processing According to the different call status5  Switch(Tm.getcallstate ()) {6      //Bells7    Casetelephonymanager.call_state_ringing:8      //get the phone number for the call9String Incomingnumber=intent.getstringextra ("Incoming_number");Ten    //If the call number is 12345678. Hang up the phone One    if("12345678". Equals (Incomingnumber)) { ACalss<telephonymanager> Telephonymanagerclass=telephonymanager.class; -    //using Java Reflection technology to obtain the method object corresponding to the Getitelephony methods -Method Telephonymethod=telephonymanagerclass.getdelaredmethod ("Getitelephony", (class[])NULL); the    //allow access to the Getitelephony method -Telephonymethod.setaccessible (true); -    //call the Getitelephony method to get the Itelephony object -Object Obj=telephonymethod.invoke (Telephonymanager. Object[])NULL); +    //gets the Endcall method corresponding to the methods object -Method Endcallmethod=obj.getclass (). GetMethod ("Endcall",NULL); +   //allow access to the Endcall method AEndcallmethod.setaccessible (true); at   //call the Endcall method to hang up the phone -Endcallmethod.invoke (obj,NULL); -  -    } -     Break; -     CaseTelephonymanager.call_state_offhook://answer the phone inLOG.D ("Call_state", "Offhook"); -    Break; to    CaseTelephonymanager.call_state_idle://Hang up the phone + closetoast (); -    Break; the  } *}

2. Finally, you need to define the broadcast receiver in the manifest file and add the permission to receive the call broadcast
Configure Incallreceiver

1 <receiver android:name= ". Incassreceiver "2 android:enable=" Treu ">3  <intent-=filter>4  < Action android:name= "Android.intent.action.PHONE_STATE"/>5  <intent-filter>6  </recevier>

Interception and judgment of incoming call information

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.