How to disable incoming & outgoing calls in android phone - android

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

Related

Is listening to ServiceState changes a valid way to detect user removing/inserting/swapping SIM card

I'm implementing a Service (but currently i'm testing the code in an Activity) that detects SIM swap/insertion/removal. When SIM change is detected, i plan to announce the event to other parts of app and store new MSISDN for later use (or clear the stored one if no sim is present).
I used to listen to android.intent.action.SIM_STATE_CHANGED broadcast, but as it's not supported by public SDK API, I've decided to switch to TelehoneManager and PhoneStateListener, listening to service state changes (as described here and here).
Unfortuntely, it seems to be unreliable. Sometimes, after swapping the SIM to a new one, and pulling it out and then inserting back again, IN_SERVICE event is not fired (the phone shows mobile network connection being active, Android Settings show service state as 'In service', but listener does not receive the event).
Also, I've noticed that after swapping to other SIM (without device restart), almost every time when I pull it out and insert back (to fire the events) and listener receives the IN_SERVICE event, SIM state (obtained by TelephonyManager.getSimState()) is not yet SIM_STATE_READY, and I can't obtain the MSISDN (using TelephonyManager.getLine1Number()). A short while after this, I could see MSISDN number in Android Settings, so it's readable, just not yet available when the event comes.
When removing and inserting back the same SIM that was in the phone when the app run for the first time, MSISDN is almost always available when IN_SERVICE event comes.
I've tested this on a few devices (all are Android 5.1) and SIM cards, the behavior is same so it's rather not faulty SIM nor device.
Of course, i have included READ_PHONE_STATE permission in a manifest.
example code:
public static void testListener(Context context) {
TelephonyManager telephonyMgr = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener listener = new PhoneStateListener(){
#Override
public void onServiceStateChanged(ServiceState serviceState) {
super.onServiceStateChanged(serviceState);
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
//StorageUtil is a simple helper class to store/load values from SharedPreferences
StorageUtil storageUtil = new StorageUtil(context);
String oldSim = storageUtil.getStringValue(LAST_SIM_STATE_KEY);
String newSim = getMsisdn(context);
String oldImsi = storageUtil.getStringValue(LAST_SIM_IMSI_KEY);
String newImsi = getImsi(context);
Log.d("SIM", MessageFormat.format("{0} === {1} -> {2} [{3} -> {4}] {5}", decodeServiceState(serviceState.getState()), oldSim, newSim, oldImsi, newImsi, tm.getSimState()));
storageUtil.storeValue(LAST_SIM_STATE_KEY, newSim);
storageUtil.storeValue(LAST_SIM_IMSI_KEY, newImsi);
}
};
telephonyMgr.listen(listener, PhoneStateListener.LISTEN_SERVICE_STATE);
}
public static String getImsi(Context context) {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
return tm.getSubscriberId();
}
public static String getMsisdn(Context context) {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
return tm.getLine1Number();
}
static private String decodeServiceState(int state) {
switch(state) {
case 0:
return "IN_SERVICE ";
case 1:
return "OUT_OF_SERVICE";
case 2:
return "EMERGENCY_ONLY";
case 3:
return "POWER_OFF ";
}
return "";
}
example logs (with comments) showing problem occurence:
http://pastebin.com/wUdGH0Hu
Is there a way to fix those issues ? They seem to be pretty random and not easy to reproduce, but they keep happening and ruining the day.
Or maybe I shouldn't use the PhoneStateListener at all ?

dual sim android phone which sim receive a call

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

Get Android IMEI without starting onCreate

I want to get my Device IMEI without calling onCreate method in my class:
public class EM extends OrmLiteBaseActivity<DatabaseHelper>
{
public void CreateMessage(Exception e, String className)
{
try
{
TelephonyManager manager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String deviceid = manager.getDeviceId();
}
catch (Exception x)
{
x.printStackTrace();
}
}
}
but I keep on getting this error: "System services not available to Activities before onCreate()". Is there any way to get IMEI without calling OnCreate?

Android how to hold and unhold active phone call

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!!

Is there any way at all to end a call in Android post 2.3?

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.

Categories

Resources