Saturday, March 23, 2013

Android programming - Call forwarding

In this post, I'm sharing with you the Android code to activate/deactivate call forwarding programatically.

The idea behing is to instruct the phone to send the MMI codes to tell the network operator to activate/deactivate the call forwaring. Please notice, that your operator must allow you to use this service otherwise it will not work.

In the exemple below, the call forwarding applies to all calls but you can use other options such as only when you are busy or you don't answer. You can find additional info here: http://en.wikipedia.org/wiki/Call_forwarding#Keypad_codes

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;

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

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.danielthat.callforwarding"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.CALL_PHONE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.danielthat.callforwarding.CallForwarding"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".CallForwarding" > 
    
    <Button
        android:id="@+id/buttonCallForwardOn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Activate Call forward" />

    <Button
        android:id="@+id/buttonCallForwardOff"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/buttonCallForwardOn"
        android:layout_below="@+id/buttonCallForwardOn"
        android:text="Cancel Call forward" />

</RelativeLayout>

7 comments: