接受来电Android Studio

时间:2018-04-10 04:49:13

标签: android telephony telephonymanager

我知道很多开发人员都会问同样的问题,关于如何在点击按钮时接听来电。

我正在处理这样的应用程序。 https://play.google.com/store/apps/details?id=com.colorphone.smooth.dialer&hl=en

我搜索了很多并成功实现了应用程序可以检测到来电的功能(通过跟随此How to detect incoming calls, in an Android device?)并打开一个用户有两个选择的活动,无论是接受来电还是拒绝来电。我不能以编程方式接受/拒绝来电的问题。我搜索了很多,但无法找到具体的解决方案。如果有人可以帮助我,我怎么能以编程方式接受来电,这将是很棒的。

2 个答案:

答案 0 :(得分:0)

可能你可以试试 通过{@link Context#getSystemService Context.getSystemService(Context.TELECOM_SERVICE)} 获取一个TelecomManager对象,然后调用acceptRingingCall()方法来回答或调用endCall()方法来拒绝调用。

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

答案 1 :(得分:0)

从Marshmallow版本开始,运行时权限是访问用户手机和用户数据所必需的。

现在,来到你的问题,要通过语法接听电话你需要2个权限
Manifest.permission.ANSWER_PHONE_CALLSManifest.permission.MODIFY_PHONE_STATE

但MODIFY_PHONE_STATE为不供第三方应用程序使用

阅读此documentation