我在一个Android应用中工作,用户接受或拒绝正常工作的视频通话。但是现在,如果我们有其他呼叫,例如应用程序或Skype,我想防止应用程序与他人发生冲突。
我然后阅读了自我管理的Connectionservice并尝试实现它。
这是完整的doc
来自活动我首次注册
TelecomManager tm = (TelecomManager)
getSystemService(Context.TELECOM_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M)
{
PhoneAccountHandle phoneAccountHandle = new PhoneAccountHandle(
new ComponentName(this.getApplicationContext(), MyConService.class),
"example");
PhoneAccount phoneAccount = PhoneAccount.builder(phoneAccountHandle, "example")
.setCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED).build();
tm.registerPhoneAccount(phoneAccount);
}
然后尝试通过以下操作来添加新调用:
PhoneAccountHandle phoneAccountHandle = new PhoneAccountHandle(
new ComponentName(this.getApplicationContext(), MyConService.class),
"example");
Bundle extras = new Bundle();
Uri uri = Uri.fromParts(PhoneAccount.SCHEME_TEL, "11223344", null);
extras.putParcelable(TelecomManager.EXTRA_INCOMING_CALL_ADDRESS, uri);
tm.addNewIncomingCall(phoneAccountHandle, extras);
app始终在tm.addnewincomingcall(PhoneAccounthandle,Extras)上崩溃,以下日志
/AndroidRuntime: FATAL EXCEPTION: main Process: com.liverep.videochat, PID: 27754
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.liverep.videochat/com.liverep.videochat.VideoChat}: java.lang.SecurityException: This PhoneAccountHandle is not enabled for this user!
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: java.lang.SecurityException: This PhoneAccountHandle is not enabled for this user!
at android.os.Parcel.readException(Parcel.java:1693)
at android.os.Parcel.readException(Parcel.java:1646)
at com.android.internal.telecom.ITelecomService$Stub$Proxy.addNewIncomingCall(ITelecomService.java:1450)
at android.telecom.TelecomManager.addNewIncomingCall(TelecomManager.java:1225)
at com.liverep.videochat.VideoChat.placeIncomingCall(VideoChat.java:792)
at com.liverep.videochat.VideoChat.call(VideoChat.java:730)
at com.liverep.videochat.VideoChat.registerForPhoneCall(VideoChat.java:716)
at com.liverep.videochat.VideoChat.onCreate(VideoChat.java:133)
at android.app.Activity.performCreate(Activity.java:6942)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2880)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
看起来您在AndroidManifest.xml中没有Manage_own_calls的权限
正确注册您的PhoneAccount,然后使用addNewIncomingCall()
。就我而言,它起作用了。
telecomManager.registerPhoneAccount(phoneAccount1);
telecomManager.addNewIncomingCall(myPhoneAccountHandle,bundle);