Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto Answering Incoming Calls in Nougat

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);
like image 447
Sanjay Bhalani Avatar asked Aug 12 '18 10:08

Sanjay Bhalani


1 Answers

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.

like image 179
Manoj Perumarath Avatar answered Sep 28 '22 07:09

Manoj Perumarath