I am working on an app name 'color caller screen'. I am showing my own activity instead of default incoming call. I have two buttons accept and reject the calls. Reject call working on all devices, but accept call not working in android API 23 to 25.Please check the code and tell me if i missed any permission or something else.
@TargetApi(Build.VERSION_CODES.M)
@SuppressLint("WrongConstant")
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public void acceptCall() {
TelecomManager telecomManager = null;
//API level >=26
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
telecomManager = (TelecomManager) getApplicationContext().getSystemService(Context.TELECOM_SERVICE);
telecomManager.acceptRingingCall();
}
//API level >=22
else if (android.os.Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) {
try {
Runtime.getRuntime().exec("input keyevent " +
Integer.toString(KeyEvent.KEYCODE_HEADSETHOOK));
} catch (IOException e) {
// Runtime.exec(String) had an I/O problem, try to fall back
String enforcedPerm = "android.permission.CALL_PRIVILEGED";
Intent btnDown = new Intent(Intent.ACTION_MEDIA_BUTTON).putExtra(
Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_HEADSETHOOK));
Intent btnUp = new Intent(Intent.ACTION_MEDIA_BUTTON).putExtra(
Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP,
KeyEvent.KEYCODE_HEADSETHOOK));
context.sendOrderedBroadcast(btnDown, enforcedPerm);
context.sendOrderedBroadcast(btnUp, enforcedPerm);
}
}
//API level =23 || API=25||API=26
if (android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.M || android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.N_MR1 || android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.N) {
try {
// Get the getITelephony() method
telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
Class<?> classTelephony = Class.forName(telephonyManager.getClass().getName());
Method method = classTelephony.getDeclaredMethod("getITelephony");
// Disable access check
method.setAccessible(true);
// Invoke getITelephony() to get the ITelephony interface
Object telephonyInterface = method.invoke(telephonyManager);
// Get the endCall method from ITelephony
Class<?> telephonyInterfaceClass = Class.forName(telephonyInterface.getClass().getName());
Method methodEndCall = telephonyInterfaceClass.getDeclaredMethod("answerRingingCall");
// Invoke endCall()
methodEndCall.invoke(telephonyInterface);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, "Errior", Toast.LENGTH_SHORT).show();
}
}
}
}
//Reject call function
@SuppressLint("PrivateApi")
private void rejectCall() throws Exception {
try {
String serviceManagerName = "android.os.ServiceManager";
String serviceManagerNativeName = "android.os.ServiceManagerNative";
String telephonyName = "com.android.internal.telephony.ITelephony";
Class<?> telephonyClass;
Class<?> telephonyStubClass;
Class<?> serviceManagerClass;
Class<?> serviceManagerNativeClass;
Method telephonyEndCall;
Object telephonyObject;
Object serviceManagerObject;
telephonyClass = Class.forName(telephonyName);
telephonyStubClass = telephonyClass.getClasses()[0];
serviceManagerClass = Class.forName(serviceManagerName);
serviceManagerNativeClass = Class.forName(serviceManagerNativeName);
Method getService = // getDefaults[29];
serviceManagerClass.getMethod("getService", String.class);
Method tempInterfaceMethod = serviceManagerNativeClass.getMethod("asInterface", IBinder.class);
Binder tmpBinder = new Binder();
tmpBinder.attachInterface(null, "fake");
serviceManagerObject = tempInterfaceMethod.invoke(null, tmpBinder);
IBinder retbinder = (IBinder) getService.invoke(serviceManagerObject, "phone");
Method serviceMethod = telephonyStubClass.getMethod("asInterface", IBinder.class);
telephonyObject = serviceMethod.invoke(null, retbinder);
telephonyEndCall = telephonyClass.getMethod("endCall");
telephonyEndCall.invoke(telephonyObject);
} catch (Exception e) {
e.printStackTrace();
Log.d("unable", "msg cant dissconect call....");
}
}
Here are Permissions
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ANSWER_PHONE_CALLS" />
<uses-permission android:name="android.permission.PROCESS_INCOMING_CALLS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.MANAGE_OWN_CALLS" />
<uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" />
<uses-feature android:name="android.hardware.telephony" />