I'm really new on Android and Kotlin.I have one Server app and one Client app. I want to send string from Server to Client app.I can start Server and then bind with Client app to Server but i cannot send my message when button click.
Here my Server :
private inner class MessageRequestHandler : Handler() {
override fun handleMessage(msg: Message) {
when (msg.what) {
SEND_MESSAGE_FLAG -> {
val bundle = Bundle()
bundle.putString("key", "Test")
val mesg = Message.obtain(null, SEND_MESSAGE_FLAG)
mesg.obj = bundle
try {
msg.replyTo.send(mesg)
} catch (e: RemoteException) {
Log.i(TAG, "Error: " + e.message)
} }
}
super.handleMessage(msg)
}
}
private val messageMessenger: Messenger = Messenger(MessageRequestHandler())
override fun onBind(intent: Intent?): IBinder {
return messageMessenger.binder
}
companion object {
private val TAG = MyService::class.java.simpleName
var SEND_MESSAGE_FLAG = 0
}
Here my Main activity of Server
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
serviceIntent = Intent(applicationContext, MyService::class.java)
}
fun sendLogin(view: View){
startService(serviceIntent)
Toast.makeText(applicationContext, "Service Started", Toast.LENGTH_SHORT).show()
}
fun sendPay(view: View){//When i press button i want to send message to Client from here
Log.i(TAG, "Self Payment")
}
fun sendLogout(view: View){
stopService(serviceIntent)
Toast.makeText(applicationContext, "Service Stopped", Toast.LENGTH_SHORT).show()
}
Here my Client Main Activity
private var serviceIntent: Intent? = null
var requestMessenger: Messenger? = null
var receiveMessenger: Messenger? = null
private var mIsBound = false
private var messageValue = 0
companion object {
const val GET_RANDOM_NUMBER_FLAG = 0
}
internal inner class recieveHandler : Handler() {
override fun handleMessage(msg: Message) {
messageValue = 0
when (msg.what) {
GET_RANDOM_NUMBER_FLAG -> {
messageValue = msg.arg1
Toast.makeText(applicationContext,"Server: $messageValue",Toast.LENGTH_LONG).show()
println(messageValue)
}
else -> {
}
}
super.handleMessage(msg)
}
}
var serviceConnection: ServiceConnection? = object : ServiceConnection {
override fun onServiceDisconnected(arg0: ComponentName) {
requestMessenger = null
receiveMessenger = null
mIsBound = false
}
override fun onServiceConnected(arg0: ComponentName, binder: IBinder) {
requestMessenger = Messenger(binder)
receiveMessenger = Messenger(recieveHandler())
mIsBound = true
}
}
fun bindServer(v: View) {
serviceConnection?.let { bindService(serviceIntent, it, BIND_AUTO_CREATE) }
if (mIsBound){
Toast.makeText(applicationContext, "Service bound", Toast.LENGTH_SHORT).show()
}
else{
Toast.makeText(applicationContext, "Service not bound", Toast.LENGTH_SHORT).show()
}
}
fun getMessage(view: View) {
if (mIsBound) {
val requestMessage = Message.obtain(null, GET_RANDOM_NUMBER_FLAG)
requestMessage.replyTo = receiveMessenger
try {
requestMessenger!!.send(requestMessage)
} catch (e: RemoteException) {
e.printStackTrace()
}
} else {
Toast.makeText(
applicationContext,
"Service Unbound, can't get message",
Toast.LENGTH_SHORT
).show()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
serviceIntent = Intent()
serviceIntent!!.component = ComponentName("com.my.server", "com.my.server.MyService")
}
What you can suggest me?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…