Can anybody let me know how to hold and unhold active phone call?
I am trying to do it using ITelephony.aidl but unable to hold the call. Here is the code I tried to hold an active call
TelephonyManager tm = (TelephonyManager) CallholdddActivity.this.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 = (ITelephony) m.invoke(tm);
//telephonyService.silenceRinger();
// here what should i write , i dont know
//System.out.println(a);
}
catch (Exception e) {
// TODO: handle exception
}
}
It's quite not possible to attain the holding options with ITelephony.aidl, since there is no such method.
It's better to use any methods using the Audio manager to find whether the call is active or not. But the drawback there is that Audio Manager could be used by other applications too. So, there will be a dilemma in knowing the Mode!!
Related
I have an android application that detect the incoming calls, i need to improve this app to work on a duos mobile device.
so i create a broadcast receiver registered in manifest for actions: phone state changed and on my onReceive method i need to check which sim receive the call. This is my code
Protected void onReceive(Context c, Intent i)
{
Int whichSim = intent
getIntExtra("simSlot",-1);
// so this methof return 0 for sim 1 and 1 for sim2
If(whichSim==-1)
WhichSim=intent.getIntExtra("com.androie.phone.extra.slot",-1);
}
I run this app on a device 4.2
2 and its working normally but when i run it on a device 4
4.4 so this method does not work, i mean that which sim return -1 in all cases. Can anyone help me?
Android does not support dual sim phone until Android 5.1 and therefore any extension to support it may be device and version specific. The following is specific for the class of phones using a variant of MultiSimTelephonyManager to handle dual sims, including Samsung duos galaxy J1 under Android 4.4.4.
Basically this class of dual sim phones use two instances of MultiSimTelephonyManager, subclassed from the regular TelephonyManager and each responsible for one SIM slot, as an interface to control the phone.
One of the means to detect the incoming call is to use the PhoneStateListener class (instead of using a receiver) to detect change in phone states. The PhoneStateListener in these phones are modified (rather than subclassed) to include a mSubscription field which should indicate the SIM slot of the listener.
Both the MultiSimTelephonyManager class and the mSubscription field of PhoneStateListener are not in the standard SDK. To compile the app to use these interface, Java Reflection is needed.
The following code should roughly illustrate how you could get the sim slot information from incoming calls. I do not have the device to test, so the code may need refinements.
Set up the listener during your initialization stage -
try {
final Class<?> tmClass = Class.forName("android.telephony.MultiSimTelephonyManager");
// MultiSimTelephonyManager Class found
// getDefault() gets the manager instances for specific slots
Method methodDefault = tmClass.getDeclaredMethod("getDefault", int.class);
methodDefault.setAccessible(true);
try {
for (int slot = 0; slot < 2; slot++) {
MultiSimTelephonyManager telephonyManagerMultiSim = (MultiSimTelephonyManager)methodDefault.invoke(null, slot);
telephonyManagerMultiSim.listen(new MultiSimListener(slot), PhoneStateListener.LISTEN_CALL_STATE);
}
} catch (ArrayIndexOutOfBoundsException e) {
// (Not tested) the getDefault method might cause the exception if there is only 1 slot
}
} catch (ClassNotFoundException e) {
//
} catch (NoSuchMethodException e) {
//
} catch (IllegalAccessException e) {
//
} catch (InvocationTargetException e) {
//
} catch (ClassCastException e) {
//
}
Override PhoneStateListener and set the mSubscription field to listen to phone state changes:
public class MultiSimListener extends PhoneStateListener {
private Field subscriptionField;
private int simSlot = -1;
public MultiSimListener (int simSlot) {
super();
try {
// Get the protected field mSubscription of PhoneStateListener and set it
subscriptionField = this.getClass().getSuperclass().getDeclaredField("mSubscription");
subscriptionField.setAccessible(true);
subscriptionField.set(this, simSlot);
this.simSlot = simSlot;
} catch (NoSuchFieldException e) {
} catch (IllegalAccessException e) {
} catch (IllegalArgumentException e) {
}
}
#Override
public void onCallStateChanged(int state, String incomingNumber) {
// Handle the event here, with state, incomingNumber and simSlot
}
}
You will also need to create a file named MultiSimTelephonyManager.java at the [project]/src/android/telephony directory.
package android.telephony;
public interface MultiSimTelephonyManager {
public void listen(PhoneStateListener listener,int events);
}
You should probably do some error checking and especially check if the phone is the target model, when using the code.
Please be warned (again) that the above would not work in most other phones and other Android versions of the same phone.
This question suggests "com.android.phone.extra.slot" may also work on some phones. Maybe try both and use the one that didn't return -1?
Have you Try this Method :-
in this method you will get 0 for 1 Sim and for second sim you will get 1.
//Working code For 4.4 Phones KitKat
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
whichSIM = intent.getExtras().getInt("subscription");
if (whichSIM == 0) {
whichSIM = 1;
editor.putLong("ChooseSim", whichSIM);
editor.commit();
// Incoming call for SIM1
} else if (whichSIM == 1) {
whichSIM = 2;
editor.putLong("ChooseSim", whichSIM);
editor.commit();
}
I am trying to use ITelephony.aidl in my application. Using this i want to answer the incoming call. I managed to use endcall() and it works perfectly. But when i try to use answerringingcall() it throws error and says that MODIFY_PHONE_STATE is needed, but cant be used since its only available for system apps.
So is there any way of making this work on android 2.3 and above?or is there any other way for answering calls ?
thanks in advance:)
here's my code.
`
public void callAnswer(){
try {
Class c = Class.forName(telephony.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(telephony);
telephonyService.answerRingingCall();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}`
and this is my ITelephony.aidl file
package com.android.internal.telephony;
interface ITelephony {
boolean endCall();
void answerRingingCall();
}
must add permission uses-permission android:name="android.permission.MODIFY_PHONE_STATE"
May be #pra16 you have found a way to accept a call. If not check out this link.The headset keyevent hack worked fine for me . I tested it on android 4.3
I want to disable the feature of incoming and outgoing calls in the Android phone.My android phone should only allow GPRS & other applications to be run.
My Android model is Samsung Ace
Please suggest me solution regarding this. Thanks in Advance.
This code will block your all call (INCOMING AND OUTGOING)
import java.lang.reflect.Method;
import android.content.Context;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import com.CallLogApp.helper.BlockNumberHelper;
import com.CallLogApp.util.UDF;
import com.android.internal.telephony.ITelephony;
public class CustomPhoneStateListener extends PhoneStateListener {
//private static final String TAG = "PhoneStateChanged";
Context context;
public CustomPhoneStateListener(Context context) {
super();
this.context = context;
}
#Override
public void onCallStateChanged(int state, String outGoingNumber) {
super.onCallStateChanged(state, outGoingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
endCallIfBlocked(outGoingNumber);
break;
case TelephonyManager.CALL_STATE_RINGING:
break;
default:
break;
}
}
private void endCallIfBlocked(String outGoingNumber) {
try {
// Java reflection to gain access to TelephonyManager's
// ITelephony getter
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
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);
if (new BlockNumberHelper(context).isBlocked(outGoingNumber))
{
telephonyService = (ITelephony) m.invoke(tm);
telephonyService.silenceRinger();
telephonyService.endCall();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Here CALL_STATE_OFFHOOK state will call each time when your call going to connect and when you received incoming call
There is no any method that you can know weather it is incoming call or outgoing call
But you can end call which is going to connect in both scenario
Anything you delevop using google's android API..
Would be located in the Applications layer of Android's architcture. But managing calls is something implemented in the Applications Framework layer. So:
If you want to block calls from an application you can't avoid the phone call state to happen, so once the telephony manager has handled the call, end it with telephonyService.endCall().
If you really need calls not to reach your application layer.. I think a custom android compilation would be needed
But have you considered that some mobile telephony providers offer "data cards" for which they block voice channels? These are the kind of cards that come in 3G mobile internet modems
I'm trying to develop something similar to AutoAnswer but it also auto hangs up when a broadcast receiver is notified. I've spent all day reading other stackoverflow question on this issue and it seems that the permission MODIFY_PHONE_STATE is restricted to System Apps, however, some posts said that using endCall(); does not require this permission.
My question has two parts:
Is endCall() still usable? Has it ever been used anywhere in any case since Android 2.3?
If it is, I need help making it work because right now it's not working. I download ITelephony.aidl from http://code.google.com/p/autoanswer/source/browse/trunk/src/com/#com/android/internal/telephony
and put it in a package. I'm a new user so I can't post images.
This is what my file tree looks like in netbeans:
And this is the code thats running in my broadcastreceiver. It's been posted numerous times here before as a solution to this problem yet I still can't get it to work :/
ITelephony telephonyService;
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Class c = Class.forName(telephony.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(telephony);
telephonyService.endCall();
}
catch (Exception e) {
//TODO: some exception handling
}
Looking through the logs, I see this error:
[PhoneIntfMgr] CMD_END_CALL: no call to hang up
EDIT: I'm brand new to android development and can't seem to find the console output for android apps - like for a call to e.printStackTrace() but I did have Toast display some text inside the catch block like this:
catch (Exception e) {
Toast toast = Toast.makeText(context, "IN CATCH BLOCK ", Toast.LENGTH_LONG);
toast.show();
e.printStackTrace();
}
But nothing shows up on the screen..
you can use this code :
telephonyManager.listen(new PhoneStateListener() {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
/* your code */
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Toast.makeText(context.getApplicationContext(), "OFFHOOK",
Toast.LENGTH_SHORT).show();
setResultData(null);
break;
case TelephonyManager.CALL_STATE_RINGING:
/* your code */
break;
}
// super.onCallStateChanged(state, incomingNumber);
}
}, PhoneStateListener.LISTEN_CALL_STATE);
When you are going to OFFHOOK state, you can see a toast that show "OFFHOOK" and then setResultData(null); is end the call.
I have a webview in my activity. Now when I use WebView.findAll() method to search text in webview it is not highlighting the matching words.
It works fine in Android 1.6 but is not working in 2.2.
There is an issue in Android issue tracker about this: http://code.google.com/p/android/issues/detail?id=9018
I placed this code right after WebView.findAll(), and it made highlighting working:
try
{
Method m = WebView.class.getMethod("setFindIsUp", Boolean.TYPE);
m.invoke(webView, true);
}
catch (Throwable ignored){}
In android 4.0.3, seems the setFindIsUp is a private method. So above code won't work. As getMethod() method won't return the private methods. Following is a work-around to call the private method which works for 4.0.3:
try{
//Can't use getMethod() as it's a private method
for(Method m : WebView.class.getDeclaredMethods()){
if(m.getName().equals("setFindIsUp")){
m.setAccessible(true);
m.invoke(view, true);
break;
}
}
}catch(Exception ignored){}