【问题标题】:How can I stop calling with codes in android?如何停止在 android 中使用代码调用?
【发布时间】:2014-07-03 06:57:34
【问题描述】:

我创建了一个呼叫号码的应用程序。它正在工作,但我想在 30 秒后关闭通话。我怎样才能做到这一点? 我的代码是:

     MyTimer = new CountDownTimer(60000, 30000) {

           public void onTick(long millisUntilFinished) {


                intent.setData(Uri.parse("tel:" + num2 + ""));
                startActivity(intent);


                            }

            public void onFinish() {


            }
        }.start();

【问题讨论】:

    标签: android


    【解决方案1】:

    你需要一些反射。首先,创建这个接口:

    package com.android.internal.telephony;
    public interface ITelephony {
        boolean endCall();
        void answerRingingCall();
        void silenceRinger();
    }
    

    并以此结束通话:

    private void endCall() {
        TelephonyManager telephonyManager = (TelephonyManager) this
                .getSystemService(Context.TELEPHONY_SERVICE);
        Class<?> myClass = null;
        try {
            myClass = Class.forName(telephonyManager.getClass().getName());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Method method = null;
        try {
            method = myClass.getDeclaredMethod("getITelephony");
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        method.setAccessible(true);
        ITelephony telephonyService = null;
        try {
            telephonyService = (ITelephony) method.invoke(telephonyManager);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        telephonyService.endCall();
    }
    

    来自here的修改答案。

    编辑:添加示例:将此添加到某些按钮单击

        //Start call
        makeCall(phoneNumber);
        //wait several seconds
        try {
            long time = holdCallSeconds * 1000;
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //hang up
        endCall();
    
    
    void makeCall(String phoneNumber) {
        Intent intent = new Intent(Intent.ACTION_CALL);
        intent.setData(Uri.parse(phoneNumber));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_FROM_BACKGROUND);
        startActivity(intent);
    }
    

    【讨论】:

      猜你喜欢
      • 2016-01-17
      • 1970-01-01
      • 2011-09-18
      • 2014-04-05
      • 2020-06-14
      • 2013-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多