Sreekanth
Sreekanth

Reputation: 21

Lollipop : Accept incoming call Programatically in android lollipop

I'm using the below code to answer an incoming call from my app(BroadcastReceiver's onReceive()) , it is working in Kitkat . The same code is not working in Lollipop.

Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
intent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
// send ordered broadcast
context.sendOrderedBroadcast(intent, null);

Please let me know how can I answer a call in Lollipop.

Thank you.

Upvotes: 0

Views: 3495

Answers (2)

Akshay Shah
Akshay Shah

Reputation: 788

This worked for me.Put this code in your broadcast reciever for action "android.intent.action.PHONE_STATE" .Your phone needs to be rooted.Generate an apk file of ur app and put it into /system/priv-apps/ .Works for Android v 5.0 i.e lollipop.

   final String LOG_TAG = "TelephonyAnswer";

    TelephonyManager tm = (TelephonyManager) context
            .getSystemService(Context.TELEPHONY_SERVICE);

    try {
        if (tm == null) {
            // this will be easier for debugging later on
            throw new NullPointerException("tm == null");
        }
        // do reflection magic
        tm.getClass().getMethod("answerRingingCall").invoke(tm);
    } catch (Exception e) {
    }

Do not forget to add permission in ur manifest.

 <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
 <uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/>

if modify_phone_state doesnt work explicitly ask for one by using this code

 ActivityCompat.requestPermissions(MainActivity.this,
                new String[]{Manifest.permission.MODIFY_PHONE_STATE},
                1);

and overide method

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 1: {

            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {


            } else {

                Toast.makeText(MainActivity.this, "Permission denied to pick call", Toast.LENGTH_SHORT).show();
            }
            return;
        }

    }
}

Upvotes: 1

Kushal
Kushal

Reputation: 8478

From Lollipop, if our application is System app or application has root access then only we can programmatically answer incoming call

For 3rd party developer application, Lollipop OS do not allow to answer programmatically to incoming call

You can check if how to do it for system app or with root access : In this Answer

Upvotes: 0

Related Questions