I have code which is supposed to answer the phone and hang up right away when someone calls the phone. I get no errors and no force close when the call comes in from a diffe
Yeah, one can't just use
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/>
because it is reserved by system, so for answering calls we use a bluetooth trick like here and for rejecting calls we can still use TelephonyManager's endCall(); function. It actually works without such permission. Dont forget to add
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
though! Cheers...
Add following code to AndroidManifest.xml
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE"/> (add it after READ_PHONE_STATE and before MODIFY_PHONE_STATE")
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
and use it for EndCall :
private void endCall()
{
Class<TelephonyManager> c = TelephonyManager.class;
Method getITelephonyMethod = null;
try {
getITelephonyMethod = c.getDeclaredMethod("getITelephony",(Class[]) null);
getITelephonyMethod.setAccessible(true);
ITelephony iTelephony = (ITelephony) getITelephonyMethod.invoke(tm, (Object[]) null);
iTelephony.endCall();
} catch (Exception e) {
}
}
I don't think you want to do telephonyService.endCall()
in getTeleService()
. You probably want to call that within the onReceive
method instead.
I'm not sure whether you want to reject all calls or only ones from certain numbers. If you are trying to only reject certain numbers, it looks like you don't have any code to check the number of the incoming call against the blocked numbers after retrieving it here:
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 <---
}
You can try changing it to the following if you want to check the number and reject calls only from the blocked numbers:
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
for (int i=0; i<blockedNumbers.length; i++) {
if (incommingNumber.equals(blockedNumbers[i])) {
telephonyService.endCall();
break;
}
}
}
Or if you just want to reject all calls, it's even simpler:
public void onReceive(Context context, Intent intent) {
telephonyService.endCall();
}