简体   繁体   English

android - 如何以编程方式接收来电?

[英]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" . 使用此代码,我可以结束任何来电,但是当我将“endCall”更改为“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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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