我无法翻译(从 Kotlin 到 Java)或理解service.connectionListener = { addConnection(it) }
在以下文件的内部类中找到的此语句:
class MainActivity : AppCompatActivity() {
private val serviceConnection = CallServiceConnection()
private var connection: TComConnection? = null
private val tcomManager: TComManager by lazy { TComManager(applicationContext) }
private val requiredPermissions =
arrayOf(
android.Manifest.permission.READ_PHONE_STATE,
android.Manifest.permission.READ_CALL_LOG
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
drop_btn.setOnClickListener { closeConnection() }
answer_btn.setOnClickListener {
val conn = connection
if (conn != null) {
conn.setActive()
} else {
Toast.makeText(applicationContext, "there is no call", Toast.LENGTH_SHORT).show()
}
}
incoming_btn.setOnClickListener {
if (!hasPermissions()) {
requestPermissions(requiredPermissions, 1)
Toast.makeText(applicationContext, "don't have permissions", Toast.LENGTH_SHORT).show()
return@setOnClickListener
}
try {
if (tcomManager.registerAccount()) {
tcomManager.addIncomingCall()
} else {
Toast.makeText(applicationContext, "account isn't registered", Toast.LENGTH_SHORT).show()
}
} catch (e: Exception) {
Toast.makeText(applicationContext, e.message, Toast.LENGTH_SHORT).show()
}
}
outgoing_btn.setOnClickListener {
if (!hasPermissions()) {
requestPermissions(requiredPermissions, 1)
Toast.makeText(applicationContext, "don't have permissions", Toast.LENGTH_SHORT).show()
return@setOnClickListener
}
try {
if (tcomManager.registerAccount()) {
tcomManager.addOutgoingCall()
} else {
Toast.makeText(applicationContext, "account isn't registered", Toast.LENGTH_SHORT).show()
}
} catch (e: Exception) {
Toast.makeText(applicationContext, e.message, Toast.LENGTH_SHORT).show()
}
}
}
override fun onStart() {
super.onStart()
Intent(this, CallService::class.java).also { intent ->
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE)
}
}
override fun onStop() {
super.onStop()
closeConnection()
unbindService(serviceConnection)
}
private fun hasPermissions(): Boolean {
return requiredPermissions.all { checkSelfPermission(it) == PackageManager.PERMISSION_GRANTED }
}
private fun closeConnection() {
connection?.let {
if (!it.isClosed()) {
it.setDisconnected(DisconnectCause(DisconnectCause.CANCELED))
}
it.listener = {}
}
val stateText = "state: no call"
state_label.text = stateText
}
private fun addConnection(newConnection: TComConnection) {
newConnection.listener = {
val stateText = "state: ${Connection.stateToString(it)}"
state_label.text = stateText
}
connection = newConnection
val stateText = "state: ${Connection.stateToString(newConnection.state)}"
state_label.text = stateText
}
inner class CallServiceConnection : ServiceConnection {
private var callService: CallService? = null
override fun onServiceConnected(name: ComponentName?, binder: IBinder?) {
val callSrvBinder = binder as CallService.CallServiceBinder
val service = callSrvBinder.getCallService()
service.connectionListener = { addConnection(it) }
callService = service
}
override fun onServiceDisconnected(name: ComponentName?) {
callService?.connectionListener = {}
}
}
}
我无法翻译上述声明(已注释掉):
public class CallServiceConnection implements ServiceConnection {
private CallService callService = null;
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
CallService.CallServiceBinder callSrvBinder = (CallService.CallServiceBinder)binder;
CallService service = callSrvBinder.getCallService();
//service.addConnection(); // <--------
callService = service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
callService.addConnection(null);
}
}
如果您需要更多参考资料,这是我正在翻译的项目: https ://github.com/dmalukov/TComTest/tree/master/app/src/main/java/com/example/tcomtest
这是问题的扩展形式: