如何检测手机何时被接听或拒绝

js5cn81o  于 2021-07-03  发布在  Java
关注(0)|答案(5)|浏览(867)

电话铃响时,我设法准备了一项活动。现在我需要知道如何取消这个活动,当我接电话或我拒绝电话。我打电话吗 EXTRA_STATE_IDLE 或者 EXTRA_STATE_OFFHOOK ?
有什么想法吗?
显示

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

incomingbroadcastreceiver java类

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

imzjd6km1#

以下是它在不同场景中所经历的状态:
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;  
 }
xiozqbni

xiozqbni2#

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

vfhzx4xs3#

如果是拨出的电话,上面的回答是完全错误的。在android系统中,没有办法检测电话是否真的有人接听(在拨出电话的情况下)。当你拨一个号码的时候 off_hook 州政府被解雇了。这是android编程的缺点之一。

nom7f22z

nom7f22z4#

下面是通过辅助功能事件检测传出呼叫的代码-
添加一个扩展 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文件夹,并添加一个名为serviceconfig.xml的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"
/>

您可以在这里找到有关serviceconfig的更多信息
现在在清单文件中添加服务,如下所示-

<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没有提供任何解决方案来检测是否有人接听电话,这是我做的最好的选择,希望它对你有用。

dgjrabp2

dgjrabp25#

在您的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;
        }
    }
}

你所需要做的就是写一些代码来检查之前的状态是否是“ringing”。如果当前状态为空闲,而前一状态为振铃,则会取消呼叫。如果当前状态为offhook,而前一状态为ringing,则他们会接听电话。

相关问题