Permission required:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Method 1: Receive incoming calls through broadcast
Define the incoming call broadcast receiving Class
Package com. zhouzijing. android. demo; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. telephony. telephonyManager; import android. util. log; public class BroadcastReceiverMgr extends BroadcastReceiver {private final String TAG = MyBroadcastReceiver. TAG; @ Override public void onReceive (Context context, Intent intent) {String action = intent. getAction (); Log. I (TAG, "[Broadcast]" + action); // if (action. equals (MyBroadcastReceiver. B _PHONE_STATE) {Log. I (TAG, "[Broadcast] PHONE_STATE"); doReceivePhone (context, intent) ;}/ *** process the phone Broadcast. * @ param context * @ param intent */public void doReceivePhone (Context context, Intent intent) {String phoneNumber = intent. getStringExtra (TelephonyManager. EXTRA_INCOMING_NUMBER); TelephonyManager telephony = (TelephonyManager) context. getSystemService (Context. TELEPHONY_SERVICE); int state = telephony. getCallState (); switch (state) {case TelephonyManager. CALL_STATE_RINGING: Log. I (TAG, "[Broadcast] waiting for answering phone =" + phoneNumber); break; case TelephonyManager. CALL_STATE_IDLE: Log. I (TAG, "[Broadcast] hanging up by phone =" + phoneNumber); break; case TelephonyManager. CALL_STATE_OFFHOOK: Log. I (TAG, "[Broadcast] In-call =" + phoneNumber); break ;}}}
Package com. zhouzijing. android. demo; import android. app. activity; import android. content. context; import android. content. intent; import android. content. intentFilter; import android. OS. bundle; import android. telephony. phoneStateListener; import android. telephony. telephonyManager; import android. util. log; import android. view. view; public class MyBroadcastReceiver extends Activity {public final static String TAG = "MyBroadcastReceiver"; public final static String B _PHONE_STATE = TelephonyManager. ACTION_PHONE_STATE_CHANGED; private BroadcastReceiverMgr mBroadcastReceiver; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. my_broadcast_receiver);} // button 1-register public void registerIt (View v) {Log. I (TAG, "registerIt"); mBroadcastReceiver = new BroadcastReceiverMgr (); IntentFilter intentFilter = new IntentFilter (); intentFilter. addAction (B _PHONE_STATE); intentFilter. setPriority (Integer. MAX_VALUE); registerReceiver (mBroadcastReceiver, intentFilter);} // button 2-Undo broadcast public void unregisterIt (View v) {Log. I (TAG, "unregisterIt"); unregisterReceiver (mBroadcastReceiver );}}
Method 2: implement through listener
Package com. zhouzijing. android. demo; import android. app. activity; import android. content. context; import android. content. intent; import android. content. intentFilter; import android. OS. bundle; import android. telephony. phoneStateListener; import android. telephony. telephonyManager; import android. util. log; import android. view. view; public class MyBroadcastReceiver extends Activity {public final static String TAG = "MyBroadcastReceiver"; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. my_broadcast_receiver);}/*** button-listener phone number * @ param v */public void createPhoneListener (View v) {TelephonyManager telephony = (TelephonyManager) getSystemService (Context. TELEPHONY_SERVICE); telephony. listen (new OnePhoneStateListener (), PhoneStateListener. LISTEN_CALL_STATE);}/*** call status listener. * @ author stephen **/class OnePhoneStateListener extends PhoneStateListener {@ Override public void onCallStateChanged (int state, String incomingNumber) {Log. I (TAG, "[Listener] Phone number:" + incomingNumber); switch (state) {case TelephonyManager. CALL_STATE_RINGING: Log. I (TAG, "[Listener] waiting for answer:" + incomingNumber); break; case TelephonyManager. CALL_STATE_IDLE: Log. I (TAG, "[Listener] hanging up:" + incomingNumber); break; case TelephonyManager. CALL_STATE_OFFHOOK: Log. I (TAG, "[Listener] In call:" + incomingNumber); break;} super. onCallStateChanged (state, incomingNumber );}}}