Tuesday, April 17, 2012

Intercept outgoing call and end call in android

 4 Steps to do this


1. Download android-src.jar API and rference it in your Project.


2. Now create package com.android.internal.telephony (exact same name of package) in your paroject's src folder.

and create interface in this package as following :
package com.android.internal.telephony;

public interface ITelephony {


boolean endCall();


void answerRingingCall();


void silenceRinger();

}

3. Create BroadCastReceiver in your project's package com.mydomain.testoutgoingcalls (your choice for packagename)

package com.mydomain.testoutgoingcalls;

import java.lang.reflect.Method;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.Toast;

import com.android.internal.telephony.ITelephony;
public class CallReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();

if(null == bundle)
return;

String phonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);


String info = "Detect Calls sample application\nOutgoing number: " + phonenumber;

Toast.makeText(context, info, Toast.LENGTH_LONG).show();



TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Class c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
com.android.internal.telephony.ITelephony telephonyService = (ITelephony) m.invoke(tm);

telephonyService.endCall();

} catch (Exception e) {
System.out.println("error on end call : "+e.getMessage());
e.printStackTrace();
}


}
}






4. Edit your AndroidManifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mydomain.testoutgoingcalls"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
<activity android:name=".MyActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<receiver android:name=".CallReceiver">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="3" />

<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
</manifest>
Now start debug and test it will work fine.

6 comments:

  1. Tested and it works fine. Anyway I have a problem getting phonenumber. Could it be because a I call CallReceiver starting service in MainActivity??? any idea?

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Hi, Its working fine...I have another doubt...how can i access "CallManager" class...please help me

    ReplyDelete
  4. I get error Permission only granted to system apps for the permission android.permission.MODIFY_PHONE_STATE in manifest file.

    ReplyDelete
  5. Hi, it works fine. But it doesn't end call in case of ussd code dialed, how can I do it?
    For example: end dial of "*155#"

    ReplyDelete
  6. Hi,
    How can we find ARFCN or gsm channel band width?
    How can we access network layer ril?

    ReplyDelete