Dynamic Android Call forwarding

2019-09-06 08:55发布

I'm developing a mobile APP and I need to forward the incoming calls to different numbers based-on contact group. I divided my contacts into groups and each group has a different call forwarding number. When I receive the incoming call I should forward it based-on its group.

I wrote this code in PhoneStateListener in Call_State_Ringing:

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);
}

}

the problem is: When first call come it will not be forwarded it is only register xxxx number in phone call forwarding. then second call will be forwarded in correct when. the question : How can I forward the incoming call based-on caller ID

1条回答
来,给爷笑一个
2楼-- · 2019-09-06 09:08

This What I found. The answer is: We can't do call forwarding after call is received on our mobile Why: Because call forwarding is done by network provider not by your device.

Process: - first you request call forwarding from network provider by using: MMI code 21. - Network provider will register your request and change call forward setting in your device. - When new call network provider will forward it directly and nothing will come to your device. as shown in this diagram

查看更多
登录 后发表回答