简体   繁体   中英

android - How receive incoming phone call programmatically?

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.

在此输入图像描述

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

Thanks in advance

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM