我正在尝试开发一个拦截来电的android应用程序。
如果该号码在联系人列表中,则如果我不希望将呼叫转移到另一个号码,电话将正常响铃。
我尝试使用MMI代码转发呼叫,但是所有呼叫都将被永久转发,并且如果电话处于振铃状态,我们将无法运行MMI呼叫。
问题:是否可以像在呼叫中心的软件中那样,不使用MMI呼叫而将呼叫转移或将来电与转移到的呼叫合并?
这是我用来拦截呼叫的BroadcastReceiver类
public class PhoneStateReceiver extends BroadcastReceiver {
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onReceive(Context context, Intent intent) {
try {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
assert state != null;
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
ApiTask backgroundTask = new ApiTask(context);
//execute the task
backgroundTask.execute("init", incomingNumber, "99999999999");
// The ApiTask check if the phone number is in the white list the call the function unconditionalForwarding(newPhoneNumber)
Toast.makeText(context, "RINGING State", Toast.LENGTH_SHORT).show();
}
if ((state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))) {
Toast.makeText(context, "Received State", Toast.LENGTH_SHORT).show();
}
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
Toast.makeText(context, "Idle State", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void unconditionalForwarding(String phoneNumber) {
Intent intent = new Intent(Intent.ACTION_CALL);
Uri uri = Uri.fromParts("tel", "**21*"+phoneNumber+"#", "");
intent.setData(uri);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
context.startActivity(intent);
} catch(SecurityException e) {
e.printStackTrace();
}
}