如何检测已接电话?

时间:2018-08-22 14:49:45

标签: java android

我正在尝试成功检测我的应用程序上自动应答的电话。我可以成功检测到振铃的呼叫并应答,但是一旦应答,就不会显示活动状态或摘机。我正在以root用户身份从外壳程序使用电话服务来应答此呼叫。现在,我为此使用了Android上的PreciseCallState,但已经尝试使用TelephonyManagerTelecomManager

我检测并接听来电的班级

@VoiceInTaskScope
public class VoiceInPhoneStateListenerSystem extends AbstractVoiceInPhoneStateListener {

    private static final String TAG = "VoicePhoneStateListenerSystem: ";

    private int mCallFinished = 0;
    private int mLastState = 0;

    private PhoneStateListenerManager mPhoneStateListenerManager;

    @Inject
    public VoiceInPhoneStateListenerSystem(@Named(TasksModule.TASKS_CONTEXT) Context context,
                                            TelephonyManager telephonyManager,
                                            PhoneStateListenerManager phoneStateListenerManager,
                                            @Named(VoiceInTaskModule.VOICE_IN_ACTION_CALL_INTENT) Intent actionCallIntent,
                                            TimerWrapper timerWrapper, Sound sound, Provider<CSFBThread> csfbThreadProvider) {
        super(context, telephonyManager, actionCallIntent, timerWrapper, sound, csfbThreadProvider);
        this.mPhoneStateListenerManager = phoneStateListenerManager;

    }

    @Override
    @CoverageIgnore
    public void startListener() {
        mPhoneStateListenerManager.listen(this, PhoneStateListener.LISTEN_PRECISE_CALL_STATE);
    }

    @Override
    @CoverageIgnore
    public void endListener() {
        mPhoneStateListenerManager.listen(this, PhoneStateListener.LISTEN_NONE);
    }

    /**
     * Obtains the precise state of the call
     * 
     * @param callState info of the state 
     */
    @Override
    public void onPreciseCallStateChanged(final PreciseCallState callState) {
        final int ringingCallState =callState.getRingingCallState();
        final int foregroundState = callState.getForegroundCallState();
        final int backgroundState = callState.getBackgroundCallState();
        final int disconnectCause = callState.getDisconnectCause() 

        if ( ringingCallState == PreciseCallState.PRECISE_CALL_STATE_INCOMING){
            Log.info(TAG +"answer");
            superSu();
        }

        if( foregroundState == PreciseCallState.PRECISE_CALL_STATE_ACTIVE){
            Log.info(TAG + "inside!!!");
            this.initTimer();
        }
}

接听电话的方法

public static void superSu() {
        try {

            Process proc = Runtime.getRuntime().exec("su");
            DataOutputStream os = new DataOutputStream(proc.getOutputStream());

            os.writeBytes("service call phone 5\n");
            Thread.currentThread().interrupt();
            os.flush();

            os.writeBytes("exit\n");
            os.flush();

            if (proc.waitFor() == 255) {
                // TODO handle being declined root access
                // 255 is the standard code for being declined root for SU
            }
        } catch (IOException e) {
            // TODO handle I/O going wrong
            // this probably means that the device isn't rooted
        } catch (InterruptedException e) {
            // don't swallow interruptions
            Thread.currentThread().interrupt();
        }
    }

1 个答案:

答案 0 :(得分:0)

多妮!!!要做的就是关闭superSu()方法上的当前线程,就像这样

 public static void superSu() {
        try {

            Process proc = Runtime.getRuntime().exec("su");
            DataOutputStream os = new DataOutputStream(proc.getOutputStream());

            os.writeBytes("service call phone 5\n");
            os.flush();

            os.writeBytes("exit\n");
            os.flush();

            Thread.currentThread().interrupt();//close current thread 

            if (proc.waitFor() == 255) {
                // TODO handle being declined root access
                // 255 is the standard code for being declined root for SU
            }
        } catch (IOException e) {
            // TODO handle I/O going wrong
            // this probably means that the device isn't rooted
        } catch (InterruptedException e) {
            // don't swallow interruptions
            Thread.currentThread().interrupt();
        }
    }