【问题标题】:Android - How to limit a call for certain duration?Android - 如何在一定时间内限制通话?
【发布时间】:2014-10-04 03:49:11
【问题描述】:

我正在创建拨打电话的 android 应用程序。我只想在一定时间后挂断或取消通话,就像我想拨打一个在 60 秒后自动挂断的电话。这在android中可能吗?这是我的代码:

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // add PhoneStateListener
    PhoneCallListener phoneListener = new PhoneCallListener();
    TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
    telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);

    Intent callIntent = new Intent(Intent.ACTION_CALL);

    callIntent.setData(Uri.parse("tel:*03362111904"));

    startActivity(callIntent);
}

private class PhoneCallListener extends PhoneStateListener {

    private boolean isPhoneCalling = false;

    String LOG_TAG = "LOGGING 123";

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

        if (TelephonyManager.CALL_STATE_RINGING == state) {
            // phone ringing
            Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
        }

        if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
            // active
            Log.i(LOG_TAG, "OFFHOOK");

            isPhoneCalling = true;
        }

        if (TelephonyManager.CALL_STATE_IDLE == state) {
            // run when class initial and phone call ended, need detect flag
            // from CALL_STATE_OFFHOOK
            Log.i(LOG_TAG, "IDLE");

            if (isPhoneCalling) {

                Log.i(LOG_TAG, "restart app");

                // restart app
                Intent i = getBaseContext().getPackageManager()
                        .getLaunchIntentForPackage(
                                getBaseContext().getPackageName());
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);

                isPhoneCalling = false;
            }

        }
    }
}

}

【问题讨论】:

    标签: android broadcastreceiver phone-call telephony telephonymanager


    【解决方案1】:

    已经提到在BroadcastReceiver 中捕获拨出电话,如果您想在拨号前结束通话,这绝对是最好的方法。

    到目前为止,我遇到的唯一挂断方法是通过 Java Reflection 挂断。由于它不是公共 API 的一部分,您应该小心使用它,而不是依赖它。对 Android 内部构成的任何更改都会有效地破坏您的应用程序。

    Prasanta Paul's blog demonstrates是怎么实现的,我总结如下。

    获取ITelephony对象:

    TelephonyManager tm = (TelephonyManager) context
            .getSystemService(Context.TELEPHONY_SERVICE);
    try {
        // Java reflection to gain access to TelephonyManager's
        // ITelephony getter
        Log.v(TAG, "Get getTeleService...");
        Class c = Class.forName(tm.getClass().getName());
        Method m = c.getDeclaredMethod("getITelephony");
        m.setAccessible(true);
        com.android.internal.telephony.ITelephony telephonyService =
                (ITelephony) m.invoke(tm);
    } catch (Exception e) {
        e.printStackTrace();
        Log.e(TAG,
                "FATAL ERROR: could not connect to telephony subsystem");
        Log.e(TAG, "Exception object: " + e);
    }
    

    结束通话:

    telephonyService.endCall();
    

    【讨论】:

    • 感谢您的回复。您提供的代码只是取消通话,但是在拨打 60 秒后如何取消通话?
    • 您可以在BroadcastReceiver中添加您的逻辑以获取通话信息
    • 您可以设置一个计时器或一个带有后延迟的处理程序,以在 60 秒后结束通话。
    • 如果您有解决方案,您可以接受答案,以便将来其他人可以得到帮助。
    猜你喜欢
    • 1970-01-01
    • 2012-09-17
    • 2015-01-09
    • 2016-09-01
    • 2017-11-27
    • 1970-01-01
    • 1970-01-01
    • 2012-03-27
    • 1970-01-01
    相关资源
    最近更新 更多