3

I want to receive incoming call pro-grammatically from my apps.

I tried some code but it's not working.

Below is my code for end 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 able to end any of the incoming call, But when I change "endCall" to "answerRingingCall". it's not receive call from my app can you please help how to resolve this issue.

Regarding Permission I am not able to apply this permission on apps.

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

See attach screen shot.

enter image description here

it's showing Permission is only granted to System apps. How to resolve this.

Thanks in advance

Roadies
  • 3,309
  • 2
  • 30
  • 46

1 Answers1

0

This may 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);
Srishti Roy
  • 576
  • 5
  • 17