2

As per android developer documentation

Android 8.0 (API level 26) introduces several new permissions related to telephony: The ANSWER_PHONE_CALLS permission allows your app to answer incoming phone calls programmatically. To handle an incoming phone call in your app, you can use the acceptRingingCall() method.

How my app give voice answer to incoming call ?

but I have not found any guide or examples about of this . so

I want automatic answer with voice(from raw folder in app) (I have need guide or example code like answering machine)

Prags
  • 2,457
  • 2
  • 21
  • 38
Turk Damgası
  • 21
  • 1
  • 2
  • Welcome to Stack Overflow! Have you tried https://stackoverflow.com/questions/26924618/how-can-incoming-calls-be-answered-programmatically-in-android-5-0-lollipop ? – malarres Jun 04 '18 at 11:05
  • Possible duplicate of [How can incoming calls be answered programmatically in Android 5.0 (Lollipop)?](https://stackoverflow.com/questions/26924618/how-can-incoming-calls-be-answered-programmatically-in-android-5-0-lollipop) – Hanzala Jun 04 '18 at 11:50
  • of course I tried and I read this link but I want example code like answering machine – Turk Damgası Jun 10 '18 at 04:06

3 Answers3

1

Updates from Google :

telecomManager.acceptRingingCall();
telecomManager.acceptRingingCall(false);
telecomManager.endCall();

All these three commands are deprecated on and from Android Q

click to verify here

Vishal Sharma
  • 616
  • 1
  • 6
  • 21
1

https://developer.android.com/reference/android/telecom/TelecomManager.html#acceptRingingCall()

Acccept Answer call is deprecated API LEVEL Q

enter image description here

try {
                    Toast.makeText(CallAnswerDialog.this, "btn_answer click", Toast.LENGTH_SHORT).show();

                    TelecomManager tm = (TelecomManager) CallAnswerDialog.this.getSystemService(Context.TELECOM_SERVICE);
                    if (tm == null) {
                        // whether you want to handle this is up to you really
                        throw new NullPointerException("tm == null");
                    }
                    tm.acceptRingingCall();   // is deprecated Now API Level Q
                    btn_answer.setVisibility(View.GONE);
                }catch (Exception e){
                    e.printStackTrace(); }
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
Keshav Gera
  • 10,807
  • 1
  • 75
  • 53
0

For answering a phone call, this is available only from Android O. Before, it might not work. Here's a code for it:

/**returns true iff we are sure that answering the phone call worked*/
fun answerCall(context: Context): Boolean {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val telecomManager = context.getSystemService(Context.TELECOM_SERVICE) as TelecomManager
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.ANSWER_PHONE_CALLS) == PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(context, Manifest.permission.MODIFY_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
            telecomManager.acceptRingingCall()
            return true
        }
        return false
    }
    // https://stackoverflow.com/a/29651130/878126
    // for HTC devices we need to broadcast a connected headset
    val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
    val broadcastConnected = "htc" == Build.MANUFACTURER.toLowerCase(Locale.US) && !audioManager.isWiredHeadsetOn
    if (broadcastConnected)
        broadcastHeadsetConnected(context, false)
    try {
        try {
            Runtime.getRuntime().exec("input keyevent " + Integer.toString(KeyEvent.KEYCODE_HEADSETHOOK))
        } catch (e: IOException) {
            // Runtime.exec(String) had an I/O problem, try to fall back
            val enforcedPerm = "android.permission.CALL_PRIVILEGED"
            val btnDown = Intent(Intent.ACTION_MEDIA_BUTTON).putExtra(Intent.EXTRA_KEY_EVENT, KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK))
            val btnUp = Intent(Intent.ACTION_MEDIA_BUTTON).putExtra(Intent.EXTRA_KEY_EVENT, KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK))
            context.sendOrderedBroadcast(btnDown, enforcedPerm)
            context.sendOrderedBroadcast(btnUp, enforcedPerm)
        }
    } finally {
        if (broadcastConnected)
            broadcastHeadsetConnected(context, false)
    }
    return false
}

For hanging up a call, this was added officially on Android P, but the workaround seems to work on most cases I've tried so far (doesn't work on Nexus 5x with Android 8.1, for example) . Here's code for this :

@SuppressLint("PrivateApi")
fun endCall(context: Context): Boolean {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        val telecomManager = context.getSystemService(Context.TELECOM_SERVICE) as TelecomManager
        if (telecomManager != null && ContextCompat.checkSelfPermission(context, Manifest.permission.ANSWER_PHONE_CALLS) == PackageManager.PERMISSION_GRANTED) {
            telecomManager.endCall()
            return true
        }
        return false
    }
    //use unofficial API for older Android versions, as written here: https://stackoverflow.com/a/8380418/878126
    try {
        val telephonyClass = Class.forName("com.android.internal.telephony.ITelephony")
        val telephonyStubClass = telephonyClass.classes[0]
        val serviceManagerClass = Class.forName("android.os.ServiceManager")
        val serviceManagerNativeClass = Class.forName("android.os.ServiceManagerNative")
        val getService = serviceManagerClass.getMethod("getService", String::class.java)
        val tempInterfaceMethod = serviceManagerNativeClass.getMethod("asInterface", IBinder::class.java)
        val tmpBinder = Binder()
        tmpBinder.attachInterface(null, "fake")
        val serviceManagerObject = tempInterfaceMethod.invoke(null, tmpBinder)
        val retbinder = getService.invoke(serviceManagerObject, "phone") as IBinder
        val serviceMethod = telephonyStubClass.getMethod("asInterface", IBinder::class.java)
        val telephonyObject = serviceMethod.invoke(null, retbinder)
        val telephonyEndCall = telephonyClass.getMethod("endCall")
        telephonyEndCall.invoke(telephonyObject)
        return false
    } catch (e: Exception) {
        e.printStackTrace()
        return false
    }
}

So, for both answering and rejecting calls, the safest way to do it, is to have it run on Android P... :(

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • 1
    But the second method does not work on Anrdoi 8.x anymore. – David Jul 04 '18 at 13:59
  • @David What do you mean? `endCall` didn't work for you? – android developer Jul 04 '18 at 14:48
  • I mean the code that uese reflection works only on Android < 8. endCall in my case says cannot resolve... despite I installed sdk 28 – David Jul 04 '18 at 15:10
  • Having installed SDK 28 doesn't mean anything in this matter. Have you tried the other solutions offered here for your device that has Android 8.x ? – android developer Jul 04 '18 at 18:08
  • Of course it means something, you need to target API 28 to be able to call endCall, I fogot to update the target flag, and yes, I tried the reflection code with 8.x, that's why I wrote it did not work. – David Jul 05 '18 at 09:52
  • Incorrect. You don't have to target to API 28 to be able to call endCall. You have to use `compileSdkVersion 28` . For the targetSdkVersion, you can set it to anything below it. Also, I was talking about having it installed. If you install it, it doesn't mean you use it in the app. You have to change the gradle file. – android developer Jul 05 '18 at 10:53
  • Yes, you are right, I meant compileSdkVersion and not target. – David Jul 05 '18 at 11:37
  • About Android 8.x, I asked if you tried other solutions that are mentioned here. Not just of mine. – android developer Jul 05 '18 at 11:59
  • So you say that it's not just answering phone calls that don't always work on devices. It's also the rejection of phone calls that don't always work. Is that right? On which device exactly have you noticed that ending calls doesn't work using any of the workarounds? – android developer Jul 05 '18 at 12:30
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/174417/discussion-between-david-and-android-developer). – David Jul 05 '18 at 12:32
  • @David I've tested today on Nexus 5x with Android 8.1 . Indeed the call rejection there doesn't work using this reflection code :( – android developer Jul 25 '18 at 08:49