如何解决如何使用 Kotlin 实现自我管理的 ConnectionService
我是一名 Android 初学者,正在尝试为我的 Cordova 应用设置 Android 代码。
我的代码运行良好。没有任何错误。显示所有日志,但未显示来电。
我已将 ConnectionService 连接到 FirebaseMessaging 服务,以在收到数据推送通知时触发来电。
我收到通知并且所有代码都成功运行。仍然没有来电界面。在 Android 9 和 10 设备上试过。
MyConnectionService.kt
@RequiresApi(Build.VERSION_CODES.M)
class MyConnectionService : ConnectionService() {
override fun onCreateOutgoingConnection(connectionManagerPhoneAccount: PhoneAccountHandle?,request: ConnectionRequest?): Connection {
Log.i("CallConnectionService","onCreateOutgoingConnection")
val conn = VoipConnection(applicationContext)
conn.setAddress(request!!.address,PRESENTATION_ALLOWED)
conn.setinitializing()
//conn.videoProvider = MyVideoProvider()
conn.setActive()
return conn
}
override fun onCreateIncomingConnection(connectionManagerPhoneAccount: PhoneAccountHandle?,"onCreateIncomingConnection")
val conn = VoipConnection(applicationContext)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
conn.connectionProperties = Connection.PROPERTY_SELF_MANAGED
}
conn.setCallerdisplayName("test call",TelecomManager.PRESENTATION_ALLOWED)
//conn.setAddress(request!!.address,PRESENTATION_ALLOWED)
conn.setRinging()
conn.setinitializing()
conn.setActive()
return conn
}
override fun onCreateIncomingConnectionFailed(connectionManagerPhoneAccount: PhoneAccountHandle?,request: ConnectionRequest?) {
super.onCreateIncomingConnectionFailed(connectionManagerPhoneAccount,request)
Log.i("CallConnectionService","create outgoing call Failed ")
}
override fun onCreateOutgoingConnectionFailed(connectionManagerPhoneAccount: PhoneAccountHandle?,request: ConnectionRequest?) {
super.onCreateOutgoingConnectionFailed(connectionManagerPhoneAccount,"create outgoing call Failed")
}
}
VoipConnection.kt
@RequiresApi(Build.VERSION_CODES.M)
class VoipConnection(ctx:Context) : Connection() {
var ctx:Context = ctx
val TAG = "CallConnection"
override fun onShowIncomingCallUi() {
Log.i(TAG,"onShowIncomingCallUi")
}
FirebaseMessagingService.kt
class MyFirebaseMessagingService : FirebaseMessagingService() {
@RequiresApi(Build.VERSION_CODES.M)
override fun onMessageReceived(remoteMessage: RemoteMessage) {
Log.d(TAG,"From: " + remoteMessage.from)
try {
val callManager = CallManager(this)
val telecomManager = this.getSystemService(Context.TELECOM_SERVICE) as TelecomManager
val componentName = ComponentName(this,MyConnectionService::class.java)
val phoneAccountHandle = PhoneAccountHandle(componentName,"Admin")
val callInfo = Bundle()
callInfo.putString("from","tester")
telecomManager.addNewIncomingCall(phoneAccountHandle,callInfo)
}
catch (e: Exception) {
Log.e("error",e.toString())
}
}
override fun onNewToken(s: String) {
Log.d(TAG,"Refreshed token: $s")
}
}
Callmanger.kt
@RequiresApi(Build.VERSION_CODES.M)
class CallManager(context: Context) {
val telecomManager: TelecomManager
var phoneAccountHandle: PhoneAccountHandle
var context: Context
val number = "3924823202"
init {
telecomManager = context.getSystemService(Context.TELECOM_SERVICE) as TelecomManager
this.context = context
val componentName = ComponentName(this.context,MyConnectionService::class.java)
phoneAccountHandle = PhoneAccountHandle(componentName,"Admin")
val phoneAccount = PhoneAccount.builder(phoneAccountHandle,"Admin")
.setCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED).build()
telecomManager.registerPhoneAccount(phoneAccount)
val intent = Intent()
intent.component = ComponentName(
"com.android.server.telecom","com.android.server.telecom.settings.EnableAccountPreferenceActivity"
)
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
Log.d("mainactivity","init TelecomManager all well");
}
}
}
解决方法
我想您应该在“onCreateIncomingConnection”实现中手动调用“onShowIncomingCallUi”,如下所示:
conn.onShowIncomingCallUi()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。