Android - How to programmatically receive an incoming phone call?

I want to receive an incoming call programmatically from my applications.

I tried the code but it doesn't work.

Below is my code for the final call.

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

    try {
        if (tm == null) {
            // this will be easier for debugging later on
            throw new NullPointerException("tm == null");
        }


        tm.getClass().getMethod("endCall").invoke(tm);//answerRingingCall

    } catch (Exception e) {
        Log.e("sdsd", "Unable to use the Telephony Manager directly.", e);
    }


}

      

Using this code I can end any incoming call, but when I change "endCall" to "answerRingingCall" . it is not accepting a call from my application, can you help to solve this problem.

As far as permission is concerned, I cannot apply this permission for apps.

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

      

See attached screenshot.

enter image description here

it shows the permission is only allowed to system apps. How to solve this problem.

Thank you in advance

+3


source to share


1 answer


This can help



private class CallStateListener extends PhoneStateListener {
  @Override
  public void onCallStateChanged(int state, String incomingNumber) {
      switch (state) {
          case TelephonyManager.CALL_STATE_RINGING:
          // called when someone is ringing to this phone

          Toast.makeText(ctx, 
                  "Incoming: "+incomingNumber, 
                  Toast.LENGTH_LONG).show();
          break;
      }
  }
}

tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);

      

0


source







All Articles