Как сделать переадресацию звонков из приложения Android - PullRequest
0 голосов
/ 26 апреля 2018

Я хочу сделать переадресацию звонков из моего приложения. Сценарий таков: когда из моего приложения поступил какой-либо вызов GSM, я хочу переадресовать этот номер на другой номер назначения.

Здесь я сделал некоторый код для переадресации звонков.но это не работает.Я создал Receiver, а также объявил в Manifest и дал разрешение Call.Но в любом случае это не работает.

Код манифеста: -

<uses-permission android:name="android.permission.CALL_PHONE" />
     <uses-permission android:name="android.permission.READ_PHONE_STATE" />
     <receiver android:name=".PhoneStateReceiver">
                <intent-filter>
                    <action android:name="android.intent.action.PHONE_STATE" />
                </intent-filter>
            </receiver>

Код получателя: -

public class PhoneStateReceiver extends BroadcastReceiver {
Context ctx;
@Override
public void onReceive(Context context, Intent intent) {
         this.ctx=context;
    try {
        System.out.println("Receiver start");
        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
        System.out.println("Incoming Number" + incomingNumber);
        System.out.println("INcoming State" + state);

        if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
            Toast.makeText(context,"Incoming Call State",Toast.LENGTH_SHORT).show();
            Toast.makeText(context,"Ringing State Number is -"+incomingNumber,Toast.LENGTH_SHORT).show();

            String fwdMobNumVar = ("**21*" + "1234567890" + "#");
            callforward(fwdMobNumVar);
        }
        if ((state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))){
            Toast.makeText(context,"Call Received State",Toast.LENGTH_SHORT).show();

        }
        if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)){

            Toast.makeText(context,"Call Idle State",Toast.LENGTH_SHORT).show();
        }
    }
    catch (Exception e){
        e.printStackTrace();
    }

}
private void callforward(String callForwardString) {


        PhoneCallListener phoneListener = new PhoneCallListener();
        TelephonyManager telephonyManager = (TelephonyManager)
                ctx.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);
        ctx.startActivity(intentCallForward);



}

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

    @Override
    public void onCallStateChanged(int state, String incomingNumber)
    {
        if (TelephonyManager.CALL_STATE_RINGING == state)
        {
            // phone ringing
            Toast.makeText(ctx,"Callforward ringing",Toast.LENGTH_SHORT).show();
        }

        if (TelephonyManager.CALL_STATE_OFFHOOK == state)
        {
            // active
            Toast.makeText(ctx,"Callstate hook",Toast.LENGTH_SHORT).show();
            isPhoneCalling = true;
        }

        if (TelephonyManager.CALL_STATE_IDLE == state)
        {
            // run when class initial and phone call ended, need detect flag
            // from CALL_STATE_OFFHOOK
            if (isPhoneCalling)
            {
                // restart app
                Toast.makeText(ctx,"Call phone forwading ",Toast.LENGTH_SHORT).show();
                Intent i = ctx.getPackageManager()
                        .getLaunchIntentForPackage(ctx.getPackageName());
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                ctx.startActivity(i);
                isPhoneCalling = false;
            }
        }
    }
}

}

...