Android如何检测拨出电话是否已接听 [英] Android how to detect if outgoing call is answered

查看:52
本文介绍了Android如何检测拨出电话是否已接听的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款仅在内部用于测试目的的应用.我已经搜索了很多,并尝试了不同帖子中建议的不同建议,但似乎没有一个对我有用.

I'm developing an app that will only be used in house for testing purpose. I have searched a lot and tried different suggestions as suggested in different post but none seems to be working for me.

我愿意接受任何建议,例如 Reflections、Accessibility Service、root 或任何其他黑客.请帮忙.

I'm open to any suggestions like Reflections, Accessibility Service, root or any other hack. Please help.

问候

推荐答案

试试这个

在 manifest.xml 文件中设置所有必需的权限.

Set all required permission in manifest.xml file.

在Service中调用这个类

Call this class in Service

public class PhoneListener extends PhoneStateListener {

private static PhoneListener instance = null;

/**
 * Must be called once on app startup
 *
 * @param context - application context
 * @return
 */
public static PhoneListener getInstance(Context context) {
    if (instance == null) {
        instance = new PhoneListener(context);
    }
    return instance;
}

public static boolean hasInstance() {
    return null != instance;
}

private final Context context;
private CallLog phoneCall;

private PhoneListener(Context context) {
    this.context = context;
}

AtomicBoolean isRecording = new AtomicBoolean();
AtomicBoolean isWhitelisted = new AtomicBoolean();


/**
 * Set the outgoing phone number
 * <p/>
 * Called by {@link MyCallReceiver}  since that is where the phone number is available in a outgoing call
 *
 * @param phoneNumber
 */
public void setOutgoing(String phoneNumber) {
    if (null == phoneCall)
        phoneCall = new CallLog();
    phoneCall.setPhoneNumber(phoneNumber);
    phoneCall.setOutgoing();
    // called here so as not to miss recording part of the conversation in TelephonyManager.CALL_STATE_OFFHOOK
    isWhitelisted.set(Database.isWhitelisted(context, phoneCall.getPhoneNumber()));
}

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

    switch (state) {
        case TelephonyManager.CALL_STATE_IDLE: // Idle... no call
            if (isRecording.get()) {
                RecordCallService.stopRecording(context);
                phoneCall = null;
                isRecording.set(false);
            }
            break;
        case TelephonyManager.CALL_STATE_OFFHOOK: // Call answered
            if (isWhitelisted.get()) {
                isWhitelisted.set(false);
                return;
            }
            if (!isRecording.get()) {
                isRecording.set(true);
                // start: Probably not ever usefull
                if (null == phoneCall)
                    phoneCall = new CallLog();
                if (!incomingNumber.isEmpty()) {
                    phoneCall.setPhoneNumber(incomingNumber);
                }
                // end: Probably not ever usefull
                RecordCallService.sartRecording(context, phoneCall);
            }
            break;
        case TelephonyManager.CALL_STATE_RINGING: // Phone ringing
            // DO NOT try RECORDING here! Leads to VERY poor quality recordings
            // I think something is not fully settled with the Incoming phone call when we get CALL_STATE_RINGING
            // a "SystemClock.sleep(1000);" in the code will allow the incoming call to stabilize and produce a good recording...(as proof of above)
            if (null == phoneCall)
                phoneCall = new CallLog();
            if (!incomingNumber.isEmpty()) {
                phoneCall.setPhoneNumber(incomingNumber);
                // called here so as not to miss recording part of the conversation in TelephonyManager.CALL_STATE_OFFHOOK
                isWhitelisted.set(Database.isWhitelisted(context, phoneCall.getPhoneNumber()));
            }
            break;
    }

}

}

并使用广播接收器

public class MyCallReceiver extends BroadcastReceiver {

public MyCallReceiver() {
}

static TelephonyManager manager;

@Override
public void onReceive(Context context, Intent intent) {
    Log.i("JLCreativeCallRecorder", "MyCallReceiver.onReceive ");

    if (!AppPreferences.getInstance(context).isRecordingEnabled()) {
        removeListener();
        return;
    }

    if (Intent.ACTION_NEW_OUTGOING_CALL.equals(intent.getAction())) {
        if (!AppPreferences.getInstance(context).isRecordingOutgoingEnabled()) {
            removeListener();
            return;
        }
        PhoneListener.getInstance(context).setOutgoing(intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER));
    } else {
        if (!AppPreferences.getInstance(context).isRecordingIncomingEnabled()) {
            removeListener();
            return;
        }
    }

    // Start Listening to the call....
    if (null == manager) {
        manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    }
    if (null != manager)
        manager.listen(PhoneListener.getInstance(context), PhoneStateListener.LISTEN_CALL_STATE);
}

private void removeListener() {
    if (null != manager) {
        if (PhoneListener.hasInstance())
            manager.listen(PhoneListener.getInstance(null), PhoneStateListener.LISTEN_NONE);
    }
}

}

我希望你能从这段代码中得到一些帮助.

I hope you get some help from this code.

谢谢

这篇关于Android如何检测拨出电话是否已接听的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆