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