I'm programming an application for filtering unsolicited calls.
The app is working properly for Android 4.2 (API level 17) to Android 7.1 (API level 25).
On some devices with Android 8.0 (API level 26) it works well (like SAMSUNG A5 2017), unfortunately, some devices with Android 8.0 (API level 26) do not work properly (SAMSUNG GALAXY S7, S7 EDGE)
I use this code (the code has been greatly simplified for the needs of this forum):
public class IncomingCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ITelephony telephonyService;
try {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING)){
TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Method m = tm.getClass().getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(tm);
if ((number != null)) {
telephonyService.endCall();
Toast.makeText(context, "Ending the call from: " + number,Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(context, "Ring " + number,Toast.LENGTH_SHORT).show();
}
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_OFFHOOK)){
Toast.makeText(context, "Answered " + number,Toast.LENGTH_SHORT).show();
}
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_IDLE)){
Toast.makeText(context, "Idle "+ number, Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
This method works seamlessly to Android 7.1.1 (including)
For Android 9 (Android P, API level 28+), this code will be used:
TelecomManager tm;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
tm = (TelecomManager)mContext.getSystemService(Context.TELECOM_SERVICE);
if (tm == null) {
throw new NullPointerException("tm == null");
}
if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ANSWER_PHONE_CALLS) != PackageManager.PERMISSION_GRANTED) {
return;
}
tm.endCall();
};
This method has been tested on Android P Preview has worked completely without any problems.
Now I ask the following question:
Is it possible to guarantee functionality on Android 8 (API levels 26 and 27) devices to block incoming calls? Do anyone have any advice on how to program this correctly?
I will be grateful for any advice.