Android: Listen outgoing/incoming call

Android: Listen outgoing/incoming call

1, Listen outgoing call
Register a broadcast receiver with action android.intent.action.NEW_OUTGOING_CALL,
but please request to use permission android.permission.PROCESS_OUTGOING_CALLS. we can get outgoing phone number by calling
String strPhoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
in BroadcastReceiver.onReceive(xxx);


2, Listen incoming call
Use TelephonyManager and PhoneStateListener

import android.app.Activity;
import android.content .Context;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

public class Telephony extends Activity
{
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        TelephonyManager mTelephonyMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        mTelephonyMgr.listen(new TeleListener(), PhoneStateListener.LISTEN_CALL_STATE);
 
  setContentView(xxxxxxxx);
 }

class TeleListener extends PhoneStateListener
{
    public void onCallStateChanged(int state, String incomingNumber)
    {  
        super.onCallStateChanged(state, incomingNumber);
        switch (state)
        {
            case TelephonyManager.CALL_STATE_IDLE:
                //CALL_STATE_IDLE;
               break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
               //CALL_STATE_OFFHOOK;
               break;
            case TelephonyManager.CALL_STATE_RINGING:
               //CALL_STATE_RINGING
               break;
            default:
               break;
         }
      }

    }
}

你可能感兴趣的:(android)