且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android:为电信注册新的PhoneAccount

更新时间:2023-02-27 07:43:31

我已经得到一些信息,我将在这里留下来作为后代.

I've got some information that I'll just leave here for posterity.

构建PhoneAccount时,如果您自己拨打和接听电话,则必须添加CAPABILITY_CALL_PROVIDER;如果要使用内置PhoneAccount拨打或接听电话,则必须添加CAPABILITY_CONNECTION_MANAGER.没有任何一个,您将不会出现在用户界面中.

When building your PhoneAccount, you must add CAPABILITY_CALL_PROVIDER if you make and receive calls on your own, or CAPABILITY_CONNECTION_MANAGER if you want to make or receive calls using the builtin PhoneAccount. Without either, you won't show up in the UI.

据我所知,没有专用的API可以检查用户是否启用了PhoneAccount.但是,可以将TelecomManager.addNewIncomingCall用于此目的.只需提供一个包含布尔值额外内容的Bundle(命名为您想要的任何名称),然后在您确实收到呼叫时将该布尔值设置为true,或者如果您只是想进行权限检查,则将该布尔值设置为false ).然后,如果您只是在进行权限检查,则ConnectionService.onCreateIncomingConnection的实现可以检查您的额外内容并返回Connection.createCanceledConnection.这不会在通话记录中注册为通话,并且铃声永远不会播放.如果未启用PhoneAccount,则会抛出addNewIncomingCall;如果未启用,则会成功.

As far as I can tell, there is no dedicated API for checking whether the user has enabled your PhoneAccount. However, you can use TelecomManager.addNewIncomingCall for this purpose. Simply provide a Bundle containing a boolean extra (named whatever you want) and set that boolean to true if you're really receiving a call or false if you just want to do a permission check (or vice-versa). Then your implementation of ConnectionService.onCreateIncomingConnection can check your extra and return Connection.createCanceledConnection if you're just doing a permission check. This does not register as a call in the call log, and the ringtone never plays. addNewIncomingCall will throw if your PhoneAccount is not enabled, and succeed if it is.

如上面的注释所述,您可以提示用户使用TelecomManager.ACTION_CHANGE_PHONE_ACCOUNTS启用您的PhoneAccount.因为用户可以随时启用或禁用PhoneAccount,所以所有需要启用PhoneAccount的操作(如addNewIncomingCall)都应放在try块中.

As noted in the comments above, you can prompt the user to enable your PhoneAccount using TelecomManager.ACTION_CHANGE_PHONE_ACCOUNTS. Because the user can enable or disable your PhoneAccount at any time, all operations that require an enabled PhoneAccount (like addNewIncomingCall) should be placed in a try block.