I am developing an App in which I need to block the teenager from using mobile while driving, I need to block the call and sms. please help . any small hints and clues will do. please help me!
-
Is there any way to do this? (http://textecution.com/) how thy can able to do this? Plz suggest me the hint. – Thiru Sep 29 '11 at 09:30
-
1it may help you: http://stackoverflow.com/questions/7121508/android-taking-complete-control-of-phone-is-it-possible-how/7121586#7121586 – Vineet Shukla Sep 29 '11 at 09:31
-
@Thiru they had done nothing but set there app as car home app – ingsaurabh Sep 29 '11 at 09:52
4 Answers
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

- 33,296
- 22
- 86
- 129
-
1@Dharmendra import com.CallLogApp.helper.BlockNumberHelper; import com.CallLogApp.util.UDF; not reolved – Hiren Patel Jul 25 '14 at 10:07
-
@user406090 these are custom function to check whether the phone number is added in block list or not. you can use your logic to check the block number. – Dharmendra Aug 24 '14 at 09:32

- 3,693
- 5
- 37
- 59
-
-
check 'Dharmendra' answer it will help to end call on Outgoing call – Hiren Dabhi Sep 29 '11 at 10:54
-
for SMS : http://mobiforge.com/developing/story/sms-messaging-android don't do any thing in sendSMS method. – Hiren Dabhi Sep 29 '11 at 11:06
-
-
To add the answer by Dharmendra saying that:
There is no any method that you can know whether it is incoming call or outgoing call
In fact, there is.
To distinguish incoming calls from outgoing calls, you have to listen to Intent.ACTION_NEW_OUTGOING_CALL
as well.
Now:
when you see first Intent.ACTION_NEW_OUTGOING_CALL
and then CALL_STATE_OFFHOOK
, it's an outgoing call;
when you first see CALL_STATE_RINGING
and then CALL_STATE_OFFHOOK
, it's an incoming call.
At second, you could either use a PhoneStateListener
or listen to TelephonyManager.ACTION_PHONE_STATE_CHANGED
. From what I could see, first the intent gets received by the BroadcastReceiver
, then the PhoneStateListener
gets notified.
final IntentFilter theFilter = new IntentFilter();
theFilter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
theFilter.addAction(Intent.ACTION_NEW_OUTGOING_CALL);
mBroadcastReceiver = new MyBroadcastReceiver();
mService.registerReceiver(mBroadcastReceiver, theFilter);
class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
...
}
}

- 7,661
- 4
- 30
- 55

- 16,368
- 4
- 94
- 127
To block outgoing calls, you need to register a PhoneStateListener
like:
telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(new MyListener(), PhoneStateListener.LISTEN_CALL_STATE);
Then define your MyListener
class like:
private class Test extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch(state)
{
case TelephonyManager.CALL_STATE_IDLE:
break;
case TelephonyManager.CALL_STATE_OFFHOOK://this case is for outgoing call
break;
case TelephonyManager.CALL_STATE_RINGING://this case is for incoming call
break;
default:
break;
}
}
}

- 7,661
- 4
- 30
- 55

- 2,005
- 1
- 14
- 15