Last active
June 5, 2024 01:54
-
-
Save robertchong/49ce92cd98e1e2c5983c to your computer and use it in GitHub Desktop.
Android programming - Call forwarding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} | |
} | |
} |
Nice Job!
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
Hi, there?
I have a question for you
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
thanks for your code.
i tried to do the same but the forwarding call dont work.
you have an idea what to do?