6

Is there any way to programmatically answer incoming calls in Android 7.0 without root privileges? I tried following way to pick up an incoming call.

 TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                  Class<?> c = Class.forName(tm.getClass().getName());
                  Method m = c.getDeclaredMethod("getITelephony");
                  m.setAccessible(true);
                  Object telephonyService = m.invoke(tm);
                  Class<?> telephonyServiceClass = Class.forName(telephonyService.getClass().getName());
                  Method endCallMethod = telephonyServiceClass.getDeclaredMethod("answerRingingCall");
                  endCallMethod.invoke(telephonyService);
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Sanjay Bhalani
  • 2,424
  • 18
  • 44
  • 1
    There's [Telecom api](https://developer.android.com/reference/android/telecom/InCallService) available since api 23 that lets your app receive all calls directly - but user will need to mark it as a default dialer app. – Pawel Aug 12 '18 at 10:46
  • 1
    have you tried looking at this question ? https://stackoverflow.com/questions/26924618/how-can-incoming-calls-be-answered-programmatically-in-android-5-0-lollipop/27084305#27084305 – a_local_nobody Jul 17 '19 at 10:28
  • Yes I do, but can't help – Sanjay Bhalani Jul 17 '19 at 10:32

1 Answers1

1

There's a weird trick you can do with notification listeners, listen to all notifications by creating a service, whenever a call is coming it'll definitely show a notification to ask the user whether to pick up or not.

Create a service

class AutoCallPickerService : NotificationListenerService() {

override fun onNotificationPosted(sbn: StatusBarNotification?) {
    super.onNotificationPosted(sbn)
    sbn?.let {
        it.notification.actions?.let { actions ->
            actions.iterator().forEach { item ->
                if (item.title.toString().equals("Answer", true)) {
                    val pendingIntent = item.actionIntent
                    pendingIntent.send()
                }
            }
        }
    }
}

override fun onNotificationRemoved(sbn: StatusBarNotification?) {
    super.onNotificationRemoved(sbn)
    }
}

Here we are assuming that the notification has an action with label as answer, thereby we are making a match and calling the corresponding intent related with it. In the launcher activity ask the permission for accessing notifications

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val intent = Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS")
    startActivity(intent)
    }
}

Finally register the service for listening to notifications

<service
            android:name=".AutoCallPickerService"
            android:label="@string/app_name"
            android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
        <intent-filter>
            <action
                    android:name="android.service.notification.NotificationListenerService" />
        </intent-filter>
</service>

If the phone model doesn't show up a notification for incoming calls this code will be of total failure.

Manoj Perumarath
  • 9,337
  • 8
  • 56
  • 77