如何从Android应用程序调用forwading

如何从Android应用程序调用forwading,android,broadcastreceiver,phone-call,telephonymanager,Android,Broadcastreceiver,Phone Call,Telephonymanager,我想从我的应用程序中进行呼叫转接。这种情况是,当任何GSM呼叫从我的应用程序中发出时,我想将该号码转接到其他指定号码 在这里,我做了一些代码呼叫转移,但它不工作。我已经创建了Receiver,也在清单中声明了它,并授予了调用权限。 但无论如何,这是行不通的 清单代码:- <uses-permission android:name="android.permission.CALL_PHONE" /> <uses-permission android:name="andr

我想从我的应用程序中进行呼叫转接。这种情况是,当任何GSM呼叫从我的应用程序中发出时,我想将该号码转接到其他指定号码

在这里,我做了一些代码呼叫转移,但它不工作。我已经创建了Receiver,也在清单中声明了它,并授予了调用权限。 但无论如何,这是行不通的

清单代码:-

<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>

}

您需要在运行时注册receiver,因为许多receiver不处理清单!可能重复的hello,我也试着从运行时做,但仍然调用未转发。感谢它的工作,但有时我得到无效的mmi代码错误。知道为什么会这样吗
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;
            }
        }
    }
}