如何检测电话何时接听或拒绝

时间:2012-03-13 13:25:55

标签: android android-intent telephony

我设法在手机响铃时准备一项活动。现在我需要知道如何取消这个活动,当我打电话或拒绝电话时。我是否打电话给EXTRA_STATE_IDLE?EXTRA_STATE_OFFHOOK?

有什么想法吗?

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



public class IncomingBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
    // If an incoming call arrives
    if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
    { //Did my work }

6 个答案:

答案 0 :(得分:25)

上述答案在拨打电话时完全错误。在Android中,没有办法检测呼叫是否实际应答(如果是呼出呼叫)。拨号时,off_hook状态被触发。这是Android编程的一个缺点。

答案 1 :(得分:21)

你的onReceive中的

PhoneStateChangeListener pscl = new PhoneStateChangeListener();
TelephonyManager tm = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(pscl, PhoneStateListener.LISTEN_CALL_STATE);

单独的课程:

private class PhoneStateChangeListener extends PhoneStateListener {
    public static boolean wasRinging;
    String LOG_TAG = "PhoneListener";
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        switch(state){
            case TelephonyManager.CALL_STATE_RINGING:
                 Log.i(LOG_TAG, "RINGING");
                 wasRinging = true;
                 break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                 Log.i(LOG_TAG, "OFFHOOK");

                 if (!wasRinging) {
                     // Start your new activity
                 } else {
                     // Cancel your old activity
                 }

                 // this should be the last piece of code before the break
                 wasRinging = true;
                 break;
            case TelephonyManager.CALL_STATE_IDLE:
                 Log.i(LOG_TAG, "IDLE");
                 // this should be the last piece of code before the break
                 wasRinging = false;
                 break;
        }
    }
}

您需要做的就是编写一些代码来检查以前的状态是否“振铃”。 如果当前状态为空闲且前一状态正在响铃,则他们取消了呼叫。 如果当前状态是摘机并且之前的状态正在响铃,他们接听了电话。

答案 2 :(得分:7)

以下是在不同场景中经历的状态:

1)接听来电

CALL_STATE_RINGING => CALL_STATE_OFFHOOK (After Answering call) => CALL_STATE_IDLE (After End call) 

2)拒绝/不应答(未接)已接来电

CALL_STATE_RINGING => CALL_STATE_IDLE (After End call)  

3)拨打电话

CALL_STATE_OFFHOOK (After dialing) => CALL_STATE_IDLE (After End call) 

<强>代码

  int prev_state=0;


  public class CustomPhoneStateListener extends PhoneStateListener {  

        private static final String TAG = "CustomPhoneStateListener";  

        @Override  
        public void onCallStateChanged(int state, String incomingNumber){  

            if(incomingNumber!=null&&incomingNumber.length()>0) incoming_nr=incomingNumber;   

            switch(state){  
                case TelephonyManager.CALL_STATE_RINGING:  
                        Log.d(TAG, "CALL_STATE_RINGING");  
                        prev_state=state;  
                        break;  
                case TelephonyManager.CALL_STATE_OFFHOOK:  
                Log.d(TAG, "CALL_STATE_OFFHOOK");  
                prev_state=state;  
                break;  
                case TelephonyManager.CALL_STATE_IDLE:  
                    Log.d(TAG, "CALL_STATE_IDLE==>"+incoming_nr);  
                    NumberDatabase database=new NumberDatabase(mContext);  
                    if((prev_state==TelephonyManager.CALL_STATE_OFFHOOK)){  
                        prev_state=state;  
                        //Answered Call which is ended  
                    }  
                    if((prev_state==TelephonyManager.CALL_STATE_RINGING)){  
                        prev_state=state;  
                        //Rejected or Missed call  
                    }  
                    break;  

            }  
        }  
    }  

在你的接收器中

onReceive(Context context, Intent intent) {  
        TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); //TelephonyManager object  
        CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener();  
        telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);          //Register our listener with TelephonyManager  

        Bundle bundle = intent.getExtras();  
        String phoneNr= bundle.getString("incoming_number");  

        mContext=context;  
 }  

答案 3 :(得分:1)

下面的

是通过辅助功能事件检测拨出电话的代码 -

在项目中添加一个扩展AccessibilityService的类 -

public class CallDetection extends AccessibilityService {
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
     acquireLock(this);
    Log.d("myaccess","after lock");
    if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED) {
        Log.d("myaccess","in window changed");
        AccessibilityNodeInfo info = event.getSource();
        if (info != null && info.getText() != null) {
            String duration = info.getText().toString();
            String zeroSeconds = String.format("%02d:%02d", new Object[]{Integer.valueOf(0), Integer.valueOf(0)});
            String firstSecond = String.format("%02d:%02d", new Object[]{Integer.valueOf(0), Integer.valueOf(1)});
            Log.d("myaccess","after calculation - "+ zeroSeconds + " --- "+ firstSecond + " --- " + duration);
            if (zeroSeconds.equals(duration) || firstSecond.equals(duration)) {
                Toast.makeText(getApplicationContext(),"Call answered",Toast.LENGTH_SHORT).show();
               // Your Code goes here
            }
            info.recycle();
        }
    }
}


@Override
protected void onServiceConnected() {
    super.onServiceConnected();
    Toast.makeText(this,"Service connected",Toast.LENGTH_SHORT).show();
    AccessibilityServiceInfo info = new AccessibilityServiceInfo();
    info.eventTypes = AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED;
    info.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;
    info.notificationTimeout = 0;
    info.packageNames = null;
    setServiceInfo(info);
}

@Override
public void onInterrupt() {

}
}

但要使函数event.getSource()正常工作,您必须通过xml指定一些服务配置,因此在项目中创建 xml 文件夹并添加名为的xml文件serviceconfig.xml (您可以提供任何您想要的名称。

serviceconfig的内容低于 -

<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:description="@string/callDetection"
android:accessibilityEventTypes="typeWindowContentChanged"
android:notificationTimeout="100"
android:canRetrieveWindowContent="true"
/>

您可以在Here

中找到有关 serviceconfig 的更多信息

现在在您的 Manifest 文件中添加您的服务 -

<service android:name=".CallDetection"
        android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
        android:label="@string/callDetection">
        <intent-filter>
            <action android:name="android.accessibilityservice.AccessibilityService" />
        </intent-filter>
        <meta-data
            android:name="android.accessibilityservice"
            android:resource="@xml/serviceconfig" />
</service>

完成后,只需运行应用并转到手机中的辅助功能设置,您就会找到一个名为检测的选项(或任何名称作为您的服务说明),将其打开以为您提供辅助功能。

现在,当接听电话时,您将看到祝酒词。

您可以在此处编码所需的任何代码,也可以在活动中调用回调函数

最重要的 - 不要拨打你的通话窗口(Android拨号窗口),直到接听电话,否则无法使用。

注意 - 由于Android没有提供任何解决方案来检测呼叫是否得到应答,这是我做过的最佳选择,希望它对你有用。

答案 4 :(得分:0)

//
public class myService extends InCallService 
{
    // Here... :)
    @Override public void onCanAddCallChanged(boolean canAddCall) 
    {
        super.onCanAddCallChanged(canAddCall);  
    }
}

答案 5 :(得分:0)

要检测是否有来电,您可以检测“hello”语音。 “hello”语音是呼叫进度频率之外的频率(语音活动)。作为参考,您可以查看此数据表部分:https://www.cmlmicro.com/products/call-progress-and-voice-detector/