Skip to content

Instantly share code, notes, and snippets.

@robertchong
Last active June 5, 2024 01:54
Show Gist options
  • Save robertchong/49ce92cd98e1e2c5983c to your computer and use it in GitHub Desktop.
Save robertchong/49ce92cd98e1e2c5983c to your computer and use it in GitHub Desktop.
Android programming - Call forwarding
package com.danielthat.callforwarding;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
/*
* Android programming - Call forwarding
* See http://danielthat.blogspot.com/2013/03/android-programming-call-forwarding.html
* Also see http://stackoverflow.com/questions/3465707/call-forwarding/8132536#8132536
*/
public class CallForwarding extends Activity
{
Button buttonCallForwardOn;
Button buttonCallForwardOff;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.call_forwarding_layout);
buttonCallForwardOn = (Button) findViewById(R.id.buttonCallForwardOn);
buttonCallForwardOn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
callforward("*21*0123456789#"); // 0123456789 is the number you want to forward the calls.
}
});
buttonCallForwardOff = (Button) findViewById(R.id.buttonCallForwardOff);
buttonCallForwardOff.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
callforward("#21#");
}
});
}
private void callforward(String callForwardString)
{
PhoneCallListener phoneListener = new PhoneCallListener();
TelephonyManager telephonyManager = (TelephonyManager)
this.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);
startActivity(intentCallForward);
}
private class PhoneCallListener extends PhoneStateListener
{
private boolean isPhoneCalling = false;
@Override
public void onCallStateChanged(int state, String incomingNumber)
{
if (TelephonyManager.CALL_STATE_RINGING == state)
{
// phone ringing
}
if (TelephonyManager.CALL_STATE_OFFHOOK == state)
{
// active
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
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
isPhoneCalling = false;
}
}
}
}
}
@moshikoh28
Copy link

Hi,

thanks for your code.
i tried to do the same but the forwarding call dont work.
you have an idea what to do?

@gwqduyfqduywqi321
Copy link

Nice Job!

@princesanjivy
Copy link

Hi,

thanks for your code.
i tried to do the same but the forwarding call dont work.
you have an idea what to do?

What device you're using? It worked fine for me

@smartlucky1107
Copy link

Hi, there?

@smartlucky1107
Copy link

I have a question for you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment