如何在android中识别来电和呼出



如何在android中分别获取来电和呼出事件。事实上,我试图开发一个应用程序,如果数据库中有号码,它可以在来电时打开。但如果我从设备上打电话(呼出(,如果数据库里有号码,仍然可以打开我的应用程序。我想限制在呼出时打开我的应用程序。

我的清单包含我收到的来电如下:

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

IncomingCallReceiver:

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

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

有人能帮我区分呼出和来电吗?这样我就可以处理这些事件了。

提前谢谢。

我想限制在呼出时打开我的应用程序。

在CCD_ 1上检查先前的状态是CCD_ 2还是CCD_
在后一种情况下,继续打开应用程序,否则不执行任何操作

在接收器中添加<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>

最新更新