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