`
notfatboy
  • 浏览: 235663 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

    }
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics