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! You can find the APK here...
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.
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.
56
comments:
said...
said...
said...
said...
said...
said...
said...
said...
said...
said...
said...
said...
said...
said...
said...
said...
said...
said...
said...
said...
said...
said...
said...
said...
said...
said...
said...
said...
said...
said...
said...
said...
said...
said...
said...
said...
said...
said...
said...
said...
said...