Android call monitoring and power-down monitoring

Source: Internet
Author: User

I think that writing articles must be written to be useful, you have to have their own ideas, about the call to the electric monitoring will follow the following three questions to expand

1. What is the use of monitoring incoming calls to power?

2, how to monitor, call to the same way to monitor the electricity?

3, actual combat, what needs special attention to the place?

Listening to incoming calls to power what?

1, can be heard on the phone to make a logo, tell the user this phone is fraud, marketing, advertising or something

2. Be able to automatically hang up for those special phones to avoid disturbing users

Incoming calls to the power of the monitoring mode (not the same way)

1. Incoming call monitoring (Phonestatelistener)

Call monitoring is the use of the Phonestatelistener class, using the way that Register the Phonestatelistener object (typically your own inheritance Phonestatelistener class to complete some encapsulation) into the System Phone Management Service (Telephonymanager)

Then through the Phonestatelistener callback method oncallstatechanged(int state, String Incomingnumber) to realize the caller's listener (Detailed implementation can refer to the Extended Reading section given later)

Register for monitoring

//Phoneservicename is the service name, usually "phone"---Context.telephony_serviceTelephonymanager Telephonymanager =(Telephonymanager) Mcontext.getsystemservice (phoneservicename);if(Telephonymanager! =NULL) {    Try {        //register for incoming call monitoringTelephonymanager.listen (Mtelephonylistener, phonestatelistener.listen_call_state); } Catch(Exception e) {//Exception Snapping    }}

Phonestatelistener oncallstatechanged method to monitor incoming call status

@Override Public void oncallstatechanged(intState , String Incomingnumber) {    Switch(state) { CaseTelephonymanager.call_state_idle://Hang up the phone             Break;  CaseTelephonymanager.call_state_offhook://Incoming call ringing             Break;  Casetelephonymanager.call_state_ringing://Call to connect             Break; default:             Break; } mcallstate=State ;}

Three State source code interpretation

/**Device Call state:no activity.*/ Public Static Final intCall_state_idle = 0;//Hang up the phone/**Device call state:ringing. A new Call arrived and is * ringing or waiting. In the latter case, another call is * already active. */ Public Static Final intcall_state_ringing = 1;//Incoming call ringing/**Device call State:off-hook. At least one call exists * This is dialing, active, or on hold, and no calls are ringing * or waiting. */ Public Static Final intCall_state_offhook = 2;//Call to connect

2, the power to monitor (through the broadcast to achieve)

// Outgoingcalllistener inherits a broadcastreceiver<receiver android:name="  Com.test.OutgoingCallListener" >    <intent-filter>        <action android:name="  Android.intent.action.PHONE_STATE"/>        <action android:name="  Android.intent.action.NEW_OUTGOING_CALL" />    </intent-filter></receiver>

In combat, what needs special attention to the place

1, Dual sim phone how to get

For dual sim phones, each card corresponds to a service and a firewallphonestatelistener, need to register their own firewallphonestatelistener for each service, the name of the service will change a little, the manufacturer may modify

 PublicArraylist<string>Getmultsimcardinfo () {//get the dual card information, this is also experience to try out, do not know what other manufacturers have a pitArraylist<string> phoneserverlist =NewArraylist<string>();  for(inti = 1; I < 3; i++) {        Try{String phoneservicename; if(Miuiutils.ismiuiv6 ()) {Phoneservicename= "phone." + string.valueof (i-1); } Else{phoneservicename= "Phone" +string.valueof (i); }            //try to get the service to see if you can getIBinder IBinder =Servicemanager.getservice (phoneservicename); if(IBinder = =NULL)Continue; Itelephony Itelephony=ITelephony.Stub.asInterface (IBinder); if(Itelephony = =NULL)Continue;        Phoneserverlist.add (Phoneservicename); } Catch(Exception e) {e.printstacktrace (); }    }    //this is the default.Phoneserverlist.add (Context.telephony_service); returnphoneserverlist;}

2. Hang up the phone

Hang up the phone using the interface provided by the system service to hang up, but hang up the phone is a method that does not guarantee success, so there will be a number of ways to hang up and use, the following provides

 Public BooleanEndcall () {BooleanCallsuccess =false; Itelephony Telephonyservice=Gettelephonyservice (); Try {        if(Telephonyservice! =NULL) {callsuccess=Telephonyservice.endcall (); }    } Catch(RemoteException e) {e.printstacktrace (); } Catch(Exception e) {e.printstacktrace (); }    if(Callsuccess = =false) {Executor ES=Executors.newsinglethreadexecutor (); Es.execute (NewRunnable () {@Override Public voidrun () {disconnectcall ();        }        }); Callsuccess=true; }    returncallsuccess;}Private BooleanDisconnectcall () {Runtime runtime=Runtime.getruntime (); Try{runtime.exec ("Service call phone 5 \ n"); } Catch(Exception exc) {exc.printstacktrace (); return false; }    return true;}//use Endcall to hang up, then use Killcall reflection call to hang again Public Static BooleanKillcall (Context context) {Try {        //Get the boring old TelephonymanagerTelephonymanager Telephonymanager =(Telephonymanager) Context.getsystemservice (Context.telephony_service); //Get The Getitelephony () methodClass Classtelephony =Class.forName (Telephonymanager.getclass (). GetName ()); Method Methodgetitelephony= Classtelephony.getdeclaredmethod ("Getitelephony"); //Ignore that the method was supposed to be privateMethodgetitelephony.setaccessible (true); //Invoke Getitelephony () to get the Itelephony interfaceObject Telephonyinterface =Methodgetitelephony.invoke (Telephonymanager); //Get The Endcall method from ItelephonyClass Telephonyinterfaceclass =Class.forName (Telephonyinterface.getclass (). GetName ()); Method Methodendcall= Telephonyinterfaceclass.getdeclaredmethod ("Endcall"); //Invoke Endcall ()Methodendcall.invoke (telephonyinterface); } Catch(Exception ex) {//many things can go wrong with reflection calls        return false; }    return true;}

3. Need permission to hang up the phone

<android:name= "Android.permission.CALL_PHONE"/>

Expand reading:

This article focuses on the overall framework of the mechanism to introduce telephone monitoring

Http://www.cnblogs.com/bastard/archive/2012/11/23/2784559.html

This article focuses on the implications of some API methods already variables

http://blog.csdn.net/skiffloveblue/article/details/7491618

Android call monitoring and power-down monitoring

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.