2

I know many developers ask the same question about how to accept incoming calls on button click.

I'm working on an application like this. https://play.google.com/store/apps/details?id=com.colorphone.smooth.dialer&hl=en

I searched a lot and successfully implement the feature where application can detect an incoming call ( by following this one How to detect incoming calls, in an Android device?) and opens up an activity where user has two choice, whether to accept the incoming call or reject it. The problem the i can't accept/reject the incoming call programmatically. I search a lot but couldn't find the specific solution. If anybody could help me on how could i accept the incoming call programmatically that will be great.

Zoe
  • 27,060
  • 21
  • 118
  • 148

2 Answers2

0

May be you can try through {@link Context#getSystemService Context.getSystemService(Context.TELECOM_SERVICE)} To get a TelecomManager object, and then call acceptRingingCall() method to answer or call endCall() method to reject call.

public class TelecomManager {

/**
 * If there is a ringing incoming call, this method accepts the call on behalf of the user.
 *
 * If the incoming call is a video call, the call will be answered with the same video state as
 * the incoming call requests.  This means, for example, that an incoming call requesting
 * {@link VideoProfile#STATE_BIDIRECTIONAL} will be answered, accepting that state.
 *
 * Requires permission: {@link android.Manifest.permission#MODIFY_PHONE_STATE} or
 * {@link android.Manifest.permission#ANSWER_PHONE_CALLS}
 */
//TODO: L-release - need to convert all invocation of ITelecmmService#answerRingingCall to use
// this method (clockwork & gearhead).
@RequiresPermission(anyOf =
        {Manifest.permission.ANSWER_PHONE_CALLS, Manifest.permission.MODIFY_PHONE_STATE})
public void acceptRingingCall() {
    try {
        if (isServiceConnected()) {
            getTelecomService().acceptRingingCall(mContext.getPackageName());
        }
    } catch (RemoteException e) {
        Log.e(TAG, "Error calling ITelecomService#acceptRingingCall", e);
    }
}
ppp wang
  • 32
  • 2
0

From the release of Marshmallow, runtime permissions are necessary to access user's phone and user's data.

Now, Coming to your question, To pickup call via grammatically you need 2 Permissions
Manifest.permission.ANSWER_PHONE_CALLS and Manifest.permission.MODIFY_PHONE_STATE

but MODIFY_PHONE_STATE is Not for use by third-party applications.

Read this documentation

Amrish Kakadiya
  • 974
  • 1
  • 7
  • 25