我想从我的应用程序进行呼叫转移,这种情况是当我的应用程序发出任何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;
}
}
}
}
}