0

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.

2

2 Answers 2

1

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;
        }

    }
}
0

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

2
  • welcome.. Is you application 3rd party or system or having root access application? Is this answer solved your problem?
    – Kushal
    Apr 21, 2015 at 3:54
  • no the prob is not solved.. as it is a 3rd party app.
    – Sreekanth
    Apr 21, 2015 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.