简体   繁体   English

如何在android中识别来电和拨出电话

[英]how to identify incoming call and outgoing call in android

how to get the events of incoming call and outgoing call in android separately. 如何分别在android中获取来电和拨出电话的事件。 Actually i was trying to develop an application that open on incoming call if number is exist in database and it work OK. 实际上我正在尝试开发一个在来电时打开的应用程序,如果数字存在于数据库中并且工作正常。 but if i call from the device(outgoing call) and if number is exist in database still it open my application. 但如果我从设备拨打电话(拨打电话),如果数字存在于数据库中,它仍会打开我的应用程序。 i want to restrict to open my application on outgoing call. 我想限制在拨打电话时打开我的应用程序。

my manifest contain i receive incoming call like this: 我的清单包含我收到这样的来电:

<receiver android:name=".IncomingCallReceiver" >
    <intent-filter >             
        <action android:name="android.intent.action.PHONE_STATE" />             
    </intent-filter>
</receiver>

IncomingCallReceiver: IncomingCallReceiver:

MyPhoneStateListener phoneListener=new MyPhoneStateListener(context);
TelephonyManager telephony = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE);

MyPhoneStateListener: MyPhoneStateListener:

public void onCallStateChanged(int state,String incomingNumber){
      switch(state) {
          case TelephonyManager.CALL_STATE_IDLE:
              Log.d("DEBUG", "IDLE");
          break;
          case TelephonyManager.CALL_STATE_OFFHOOK:
              Log.d("DEBUG", "OFFHOOK");                        
              Intent i = new Intent(context, MyMainActivity.class);
              i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              context.startActivity(i);
          break;
          case TelephonyManager.CALL_STATE_RINGING:
              Log.d("DEBUG", "RINGING");
          break;
    }
}

can anyone help me to differentiate outgoing call from incoming call so i'll handle this events. 任何人都可以帮我区分来电和来电,所以我会处理这个事件。

Thanks in advance. 提前致谢。

i want to restrict to open my application on outgoing call. 我想限制在拨打电话时打开我的应用程序。

on case TelephonyManager.CALL_STATE_OFFHOOK: check if the previous state was CALL_STATE_RINGING or CALL_STATE_IDLE (for example by settings a different flag in both cases). case TelephonyManager.CALL_STATE_OFFHOOK:检查以前的状态是否为CALL_STATE_RINGINGCALL_STATE_IDLE (例如,通过在两种情况下设置不同的标志)。
In the latter case proceed with opening your application, else do nothing 在后一种情况下,继续打开您的应用程序,否则什么也不做

add <action android:name="android.intent.action.NEW_OUTGOING_CALL" /> in your receiver and process it in your onReceive() method. 在接收器中添加<action android:name="android.intent.action.NEW_OUTGOING_CALL" />并在onReceive()方法中处理它。

<receiver android:name=".IncomingCallReceiver" >
    <intent-filter >             
       <action android:name="android.intent.action.PHONE_STATE" />
       <action android:name="android.intent.action.NEW_OUTGOING_CALL" />             
    </intent-filter>
</receiver>

The intent action android.intent.action.NEW_OUTGOING_CALL process the outgoing calls, but for the incoming call i suggest to check it this way: 意图动作android.intent.action.NEW_OUTGOING_CALL处理传出呼叫,但对于传入呼叫,我建议以这种方式检查:

if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
        //outgoing call
} else if (intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER) != null) {
        //incoming call
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM