动态Android呼叫转移

时间:2016-12-30 06:25:49

标签: java android telephony

我正在开发移动APP,我需要根据联系人群组将来电转接到不同的号码。我将联系人分成小组,每组都有不同的呼叫转移号码。当我收到来电时,我应该根据它的小组转发它。

我在CallState_Ringing中的PhoneStateListener中编写了这段代码:

package com.example.user2.callforwardnew;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;


public class CallActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TelephonyManager telephonyManager =
               (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

    PhoneStateListener callStateListener = new PhoneStateListener() {
        public void onCallStateChanged(int state, String incomingNumber){
            if(state==TelephonyManager.CALL_STATE_RINGING){

                String url = "tel:" + "**21*" + "XXXXX" + Uri.encode("#");
                Intent intent1 = (new Intent(Intent.ACTION_CALL, Uri.parse(url)));
                intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent1);
            }
            if(state==TelephonyManager.CALL_STATE_OFFHOOK){

                Toast.makeText(getApplicationContext(),"Phone is Currently in A call",
                        Toast.LENGTH_LONG).show();
            }
            if(state==TelephonyManager.CALL_STATE_IDLE){



                Toast.makeText(getApplicationContext(),"phone is neither ringing nor in a call",
                        Toast.LENGTH_LONG).show();
            }
        }
    };
    telephonyManager.listen(callStateListener,PhoneStateListener.LISTEN_CALL_STATE);
}

}

问题是:当第一次来电时它不会被转发它只是在电话呼叫转发中注册xxxx号码。然后第二次通话将在正确时转发。 问题: 如何根据来电显示转发来电

1 个答案:

答案 0 :(得分:0)

这是我发现的。 答案是:在我们的手机上收到电话后,我们无法进行呼叫转接 原因:因为呼叫转发是由网络提供商完成的,而不是您的设备。

过程:   - 首先,您使用以下命令从网络提供商请求呼叫转移:MMI代码21。   - 网络提供商将在您的设备中注册您的请求并更改呼叫转移设置。   - 当新的呼叫网络提供商直接转发它时,您的设备将不会发送任何内容。  as shown in this diagram