当对讲激活时,如何以编程方式接听电话

时间:2017-03-02 14:21:09

标签: android talkback programmatically

我可以通过以下代码接听电话:

    public void answerPhoneHeadsethook(Context context, Intent intent) {

    String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
    if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {

        try {

            if (isAccessibilitySettingsOn(mContext)) {

                /**
                 * when talk-back is on
                 */


            } else {

                /**
                 * when talk-back is off
                 */

                Process process = Runtime.getRuntime().exec("input keyevent " + Integer.toString(KeyEvent.KEYCODE_HEADSETHOOK));
                Log.i("process error", "" + process.getOutputStream());
            }


        } catch (IOException e) {
            // Runtime.exec(String) had an I/O problem, try to fall back
            String enforcedPerm = "android.permission.CALL_PRIVILEGED";
            Intent btnDown = new Intent(Intent.ACTION_MEDIA_BUTTON).putExtra(
                    Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN,
                            KeyEvent.KEYCODE_HEADSETHOOK));
            Intent btnUp = new Intent(Intent.ACTION_MEDIA_BUTTON).putExtra(
                    Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP,
                            KeyEvent.KEYCODE_HEADSETHOOK));

            context.sendOrderedBroadcast(btnDown, enforcedPerm);
            context.sendOrderedBroadcast(btnUp, enforcedPerm);
        }

    }
}

并且此代码已成功运行。但是当对讲模式处于活动状态时,此代码无效。当对讲激活时,我想接听电话。

0 个答案:

没有答案