Android Phone monitoring and interception

Source: Internet
Author: User
Tags dialpad

First, in the Manifest.xml file to obtain the listening phone permissions, registered monitoring phone activity

<receiverAndroid:name=". Phonereceiver ">            <Intent-filterandroid:priority= "+">                <ActionAndroid:name= "Android.intent.action.PHONE_STATE"/>                <ActionAndroid:name= "Android.intent.action.NEW_OUTGOING_CALL" />            </Intent-filter>        </receiver>
1  <!--  - 2     <android:name= " Android.permission.READ_PHONE_STATE "/>

Second, the main problem in the implementation process is the interface Itelephony, is the Android system phone class Telephonymanager provides to the upper application user and telephony to interact with the interface. , you must use Aidl (Android Interface definition Language, which is the Android Interface Definition language).

The specific operation is in the SRC file in a new package, the package named: Com.android.internal.telephony, in this package, a new named Itelephony.aidl, this article mainly through the interface to obtain the Endcall () function to intercept the phone. Finally rebuild project to automatically generate Itelephony.java files in the same directory as packets in the Android studio project

/** Copyright (C) The Android Open Source project** Licensed under the Apache License, Version 2.0 (the "License"); * Except in compliance with the license.* your may obtain a copy of the License at** HTTP://WW w.apache.org/licenses/license-2.0 * * Unless required by applicable law or agreed to in writing, software* distributed unde R the License is distributed on a "as is" basis,* without warranties OR CONDITIONS of any KIND, either express or implied . * See the License for the specific language governing permissions and* limitations under the License.*/package Com.androi D.internal.telephony;import Android.os.bundle;import Java.util.list;import Android.telephony.NeighboringCellInfo  ;/*** Interface used to interact with the phone.  Mostly this was used by the * Telephonymanager class. A few places is still using this directly.* "clean them" if possible and use Telephonymanager insteadl.** {@hide} */interface itelephony {/** * Dial a number. This doesn ' t place the call.     IT Displays * the Dialer screen. * @param number The number to be dialed.     If NULL, this * would display of the Dialer screen with no number pre-filled.    */void Dial (String number);     /** * Place a call to the specified number.     * @param number The number to be called.    */void Call (String number);     /** * If There is currently a call in progress, show the call screen.  * The DTMF Dialpad may or May is visible initially, depending on * whether it is up and the user last exited the     Incallscreen.     * * @return True if the call is shown.    */Boolean Showcallscreen (); /** * Variation of Showcallscreen () that also specifies whether the * DTMF Dialpad should is initially visible whe     n the Incallscreen * comes up. * * @param showdialpad If True, make the dialpad visible initially, * otherwise hide the Dialpa     D initially. * @return True if the call SCReen was shown.    * * @see Showcallscreen */Boolean Showcallscreenwithdialpad (Boolean showdialpad);    /** * End call or go to the Home screens * * @return Whether it hung up */Boolean endcall ();     /** * Answer the currently-ringing call.  * If there ' s already a current active call, that's call would be * automatically put on hold.     If both lines is currently in use, the * current active call would be ended.  * * Todo:provide a flag to let the caller specify "what policy to use * if both lines is in use. (The current behavior are hardwired to * "answer incoming, end ongoing", which are how the call button * is specced     to behave.)     * * Todo:this should is 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 have already been silenced, or * even if there ' no incoming call.     (If So, the This method would do nothing.)     * * Todo:this should is a oneway call too (see above).     * (actually *all* the methods here, return void can * probably be oneway.)    */void Silenceringer ();     /** * Check If we are in either a active or holding call * @return True if the phone was offhook.    */Boolean Isoffhook ();     /** * Check If an incoming phone call are ringing or call waiting.     * @return True if the phone is ringing.    */Boolean isringing ();     /** * Check If the phone is idle.     * @return True if the phone is IDLE.    */Boolean isidle ();     /** * Check to see if the radio are on or not.     * @return Returns TRUE if the radio is on.    */Boolean Isradioon ();     /** * Check if the SIM pin lock is enabled.     * @return True if the SIM pin lock is enabled. * * BooLean issimpinenabled ();     /** * Cancels the missed calls notification.     */void Cancelmissedcallsnotification ();  /** * supply a PIN to unlock the SIM.     Blocks until a result is determined.     * @param pin the pin to check.     * @return Whether the operation was a success.    */Boolean Supplypin (String pin);  /** * Handles PIN MMI commands (PIN/PIN2/PUK/PUK2), which is initiated * without SEND (so<Code>Dial</Code> is not appropriate).     * * @param dialstring the MMI command to be executed.     * @return True if MMI command is executed.    */Boolean Handlepinmmi (String dialstring);     /** * Toggles the radio on or off.    */void Toggleradioonoff ();    /** * Set the radio to ON or off */Boolean Setradio (Boolean turnOn);    /** * Request to update the location information on service state */void updateservicelocation ();     /** * Enable Location update notifications.    */void Enablelocationupdates ();     /** * Disable Location update notifications.    */void Disablelocationupdates ();     /** * Enable a specific APN type.    */int Enableapntype (String type);     /** * Disable a specific APN type.    */int Disableapntype (String type);     /** * Allow mobile data connections.    */Boolean enabledataconnectivity ();     /** * Disallow mobile data connections.    */Boolean disabledataconnectivity (); /** * RePort whether data connectivity is possible.    */Boolean isdataconnectivitypossible ();    Bundle getcelllocation ();     /** * Returns The neighboring cell information of the device. */List<Neighboringcellinfo>Getneighboringcellinfo ();     int getcallstate ();     int getdataactivity (); int getdatastate ();}

Third, the main implementation of the Code

Import Com.android.internal.telephony.ITelephony in activity, you can use the Itelephony interface

Package Com.example.administrator.locker2;import Java.lang.reflect.method;import Android.app.service;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import Android.os.ibinder;import Android.telephony.phonestatelistener;import Android.telephony.telephonymanager;import Android.util.log;import Com.android.internal.telephony.itelephony;public class Phonereceiver extends    broadcastreceiver {String TAG = "Phonereceiver"; @Override public void OnReceive (context context, Intent Intent) {if (Intent.getaction (). Equals (Intent.action_ne W_outgoing_call)) {//If you are going to power (dial out)} else {Telephonymanager TM = (Telephonymanager) context.ge            Tsystemservice (Service.telephony_service);        Set up a listener tm.listen (listener, phonestatelistener.listen_call_state); }} Phonestatelistener Listener = new Phonestatelistener () {@Override public void oncallstatechanged ( int state, StRing incomingnumber) {//state current status Incomingnumber, does not seem to be the power of the API super.oncallstatechanged, incom            Ingnumber);                Switch (state) {//Phone has idle case TelephonyManager.CALL_STATE_IDLE:break;                The phone was suspended case TelephonyManager.CALL_STATE_OFFHOOK:break; When the phone calls in case TELEPHONYMANAGER.CALL_STATE_RINGING:LOG.E (TAG, "caller number is:" + incomingnumber)                    ;                        If the number belongs to the blacklist if (Incomingnumber.equals ("*********")) {//TODO: block if it is blacklisted             Stopcall ();            } break;    }        }    }; public void Stopcall () {try {method = Class.forName ("Android.os.ServiceManager"). GetMethod ("Get            Service ", String.class); Gets the proxy for the remote Telephony_service IBinder object IBinder binder = (ibinder) Method.invokE (null, new object[] {"Phone"});            Converts the proxy of the IBinder object to the Itelephony object Itelephony telephony = ITelephony.Stub.asInterface (binder);            Hang up the phone telephony.endcall ();        Telephony.cancelmissedcallsnotification (); } catch (Exception e) {}}}

Four, the real machine test, when the computer turned on the application, the Interception number dial-up indicates that the phone you dialed is on the call

Android Phone monitoring and interception

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.