Bulk Emailing

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Friday, 3 September 2010

Call Control in Android

Posted on 04:15 by Unknown
This tutorial is for those who want to control Phone Call in Android OS. Programmatic approach to Accept or Reject call without user intervention.
Kindly note, this approach uses Java Reflection to call methods of an internal class of Android Telephony Framework and might not work with all versions of Android OS. The core concept has been explained in this open code
1st thing 1st, Give the permission...
You need to define 3 User Permissions to handle call related functionality- (due to limitation of Blogger XML Tag marking has been replaced with “[“ and “]”)
[uses-permission android:name="android.permission.READ_PHONE_STATE"/]
[uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/] (For answerRingingCall() method)
[uses-permission android:name="android.permission.CALL_PHONE"/] (For endCall() method)
Define a Receiver...
Create a Receiver which accepts broadcasts with intent action android.intent.action.PHONE_STATE, define following in the Manifest,
 [receiver android:name=".PhoneCall"]
        [intent-filter]
            [action android:name="android.intent.action.PHONE_STATE"/]  
        [/intent-filter]
[/receiver]

Handle Broadcast inside your Receiver...
Override onReceive() method and using Java Reflection try to get the Instance of one of hidden class of Android Telephony Framework- com.android.internal.telephony.ITelephony
ITelephony defines a set of useful methods to control the Call state, which are hidden from general Android API.
private void getTeleService() {
        TelephonyManager tm = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        try {
            // Java reflection to gain access to TelephonyManager's
            // ITelephony getter
            Log.v(TAG, "Get getTeleService...");
            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);
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG,
                    "FATAL ERROR: could not connect to telephony subsystem");
            Log.e(TAG, "Exception object: " + e);
        }
}
Now, you can use telephonyService (which is an instance of com.android.internal.telephony.ITelephony) to "Accept"/"Reject" call and many other operations.
// Accept Call
telephonyService.answerRingingCall();
// Reject Call
telephonyService.endCall();

Sample Flow for Call Barring Application
1.     Define a set of numbers to be blocked-
String[] blockedNumbers = {"+919811284018", "+919811284012"};
2.     Read incoming phone number
String incommingNumber;
@Override
public void onReceive(Context context, Intent intent) {
Bundle b = intent.getExtras();
incommingNumber = b.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
// Additional Step
// Check whether this number matches with your defined Block List
// If yes, Reject the Call
}
3.     Reject this Call
telephonyService.endCall();

Hope it helps! 
I have developed one Widget PhoneAway and it uses the above explained concept. Feel free to download its source (through SVN) and APK.

Update: 
To mute an ongoing call, you can use com.android.internal.telephony.ITelephony.setMute(boolean ). You need to make sure the Call State is CALL_STATE_OFFHOOK
e.g.
continuing above code base-
if(tm.getCallState() == TelephonyManager.CALL_STATE_OFFHOOK){
  // mute the call
  telephonyService.setMute(true);
}


Kindly note: I couldn't test properly the above code. Please let me know if the Mute functionality works.

Email ThisBlogThis!Share to XShare to Facebook
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • CityWeather
    Update: Release 1.1 has been uploaded. It will now provide weekly forecast of your selected cities. Download   CityWeather is an Android...

Blog Archive

  • ►  2013 (6)
    • ►  September (2)
    • ►  May (1)
    • ►  April (1)
    • ►  February (1)
    • ►  January (1)
  • ►  2012 (4)
    • ►  July (2)
    • ►  March (1)
    • ►  January (1)
  • ►  2011 (11)
    • ►  November (1)
    • ►  October (2)
    • ►  August (1)
    • ►  June (1)
    • ►  April (2)
    • ►  March (3)
    • ►  January (1)
  • ▼  2010 (27)
    • ►  December (2)
    • ►  November (3)
    • ▼  September (2)
      • Android Contacts DB Internal
      • Call Control in Android
    • ►  August (4)
    • ►  July (4)
    • ►  June (7)
    • ►  May (5)
Powered by Blogger.

About Me

Unknown
View my complete profile