Android Inbound phone monitoring (incoming call monitoring) turn

Source: Internet
Author: User

Permissions Required:

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

Way one: Receive incoming calls via broadcast

 PackageCom.zhouzijing.android.demo;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.telephony.TelephonyManager;ImportAndroid.util.Log; Public classBroadcastreceivermgrextendsBroadcastreceiver {Private FinalString TAG =Mybroadcastreceiver.tag; @Override Public voidOnReceive (Context context, Intent Intent) {String action=intent.getaction (); LOG.I (TAG,"[Broadcast]" +action); //Incoming Calls        if(Action.equals (mybroadcastreceiver.b_phone_state)) {log.i (TAG,"[Broadcast]phone_state]);        Doreceivephone (context,intent); }    }        /*** Processing of telephone broadcasts. * @paramContext *@paramIntent*/     Public voidDoreceivephone (Context context, Intent Intent) {String PhoneNumber=Intent.getstringextra (Telephonymanager.extra_incoming_number); Telephonymanager Telephony=(Telephonymanager) Context.getsystemservice (Context.telephony_service); intState =telephony.getcallstate (); Switch(state) { Casetelephonymanager.call_state_ringing:log.i (TAG,"[broadcast] waiting to be answered =" +PhoneNumber);  Break;  Casetelephonymanager.call_state_idle:log.i (TAG,"[Broadcast] phone hangs up =" +PhoneNumber);  Break;  Casetelephonymanager.call_state_offhook:log.i (TAG,"[broadcast] in call =" +PhoneNumber);  Break; }    }}

Defining the Actitvity Class

 PackageCom.zhouzijing.android.demo;Importandroid.app.Activity;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.content.IntentFilter;ImportAndroid.os.Bundle;ImportAndroid.telephony.PhoneStateListener;ImportAndroid.telephony.TelephonyManager;ImportAndroid.util.Log;ImportAndroid.view.View; Public classMybroadcastreceiverextendsActivity { Public Final StaticString TAG = "Mybroadcastreceiver";  Public Final StaticString b_phone_state =telephonymanager.action_phone_state_changed; PrivateBroadcastreceivermgr Mbroadcastreceiver; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.my_broadcast_receiver); }        //Button 1-registering the broadcast     Public voidRegisterit (View v) {log.i (TAG,"Registerit"); Mbroadcastreceiver=Newbroadcastreceivermgr (); Intentfilter Intentfilter=NewIntentfilter ();        Intentfilter.addaction (b_phone_state);        Intentfilter.setpriority (Integer.max_value);    Registerreceiver (Mbroadcastreceiver, Intentfilter); }        //Button 2-Cancel broadcast     Public voidUnregisterit (View v) {log.i (TAG,"Unregisterit");    Unregisterreceiver (Mbroadcastreceiver); }    }

Way two: Through the listener to achieve

 PackageCom.zhouzijing.android.demo;Importandroid.app.Activity;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.content.IntentFilter;ImportAndroid.os.Bundle;ImportAndroid.telephony.PhoneStateListener;ImportAndroid.telephony.TelephonyManager;ImportAndroid.util.Log;ImportAndroid.view.View; Public classMybroadcastreceiverextendsActivity { Public Final StaticString TAG = "Mybroadcastreceiver"; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.my_broadcast_receiver); }        /*** button-Monitor phone *@paramv*/     Public voidCreatephonelistener (View v) {Telephonymanager telephony=(Telephonymanager) Getsystemservice (Context.telephony_service); Telephony.listen (NewOnephonestatelistener (), phonestatelistener.listen_call_state); }        /*** Phone status monitoring. * @authorStephen **/    classOnephonestatelistenerextendsphonestatelistener{@Override Public voidOncallstatechanged (intState , String Incomingnumber) {log.i (TAG,"[Listener] Phone number:" +incomingnumber); Switch(state) { Casetelephonymanager.call_state_ringing:log.i (TAG,[Listener] Waiting to receive phone: "+incomingnumber);  Break;  Casetelephonymanager.call_state_idle:log.i (TAG,"[Listener] phone hangs up:" +incomingnumber);  Break;  Casetelephonymanager.call_state_offhook:log.i (TAG,"[Listener] in call:" +incomingnumber);  Break; }            Super. oncallstatechanged (State, Incomingnumber); }    }            }

Reprint http://stephen830.iteye.com/blog/1181010

Listening for Android incoming calls (incoming call listening) turn

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.