是否可以在android中以编程方式调用divert

时间:2016-06-27 11:08:58

标签: android phone-call telephonymanager

当我在我的应用程序中按下前进按钮时,如何将来电转接/转发到另一个号码。我得到的问题是来电时无法拨打电话。请建议可能的方式。

 private void callforward(String callForwardString) {
    PhoneCallListener phoneListener = new PhoneCallListener();
    TelephonyManager telephonyManager = (TelephonyManager)
            this.getSystemService(Context.TELEPHONY_SERVICE);
    telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);

    Intent intentCallForward = new Intent(Intent.ACTION_CALL);
    Uri mmiCode = Uri.fromParts("tel", callForwardString, "#");
    intentCallForward.setData(mmiCode);
    if (ActivityCompat.checkSelfPermission(this,    Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    startActivity(intentCallForward);
}

private class PhoneCallListener extends PhoneStateListener
{
    private boolean isPhoneCalling = false;

    @Override
    public void onCallStateChanged(int state, String incomingNumber)
    {
        if (TelephonyManager.CALL_STATE_RINGING == state)
        {
            // phone ringing
            if (isPhoneCalling)
            {
                // restart app
                Intent i = getBaseContext().getPackageManager()
                        .getLaunchIntentForPackage(getBaseContext().getPackageName());
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
                isPhoneCalling = false;
            }
        }

        if (TelephonyManager.CALL_STATE_OFFHOOK == state)
        {
            // active
            isPhoneCalling = true;
        }

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

        }
    }
}

0 个答案:

没有答案