Android listening phone calls

Source: Internet
Author: User

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 );}}}

 

 

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.