第一步 创建一个RemoteService 服务进程代码
注意的地方:bundle解析实体消息的时候要设置bundle的ClassLoader。
import android.app.Service
import android.content.Intent
import android.os.*
import android.util.Log
class RemoteService : Service() {
val handler = MyHandler(Looper.getMainLooper())
val messenger = Messenger(handler)
var replyMessenger :Messenger?=null
override fun onBind(intent: Intent?): IBinder? {
return messenger.binder
}
inner class MyHandler(looper: Looper) : Handler(looper) {
override fun handleMessage(msg: Message?) {
super.handleMessage(msg)
replyMessenger = msg?.replyTo
val bundle = msg?.data
//这里是需要注意的地方。要设置一下classLoader否则回抛出异常
// android.os.BadParcelableException: ClassNotFoundException when unmarshalling: face.com.myaidltest.MyMessage2
bundle?.classLoader =MyMessage::class.java.classLoader
val myMessage = bundle?.getParcelable("message");
Log.d("RemoteService","服务端接收到的数据=" + myMessage?.content + myMessage?.isConnect)
//3秒之后 自动回复另一个进程信息。
handler.postDelayed({
val myMessage = MyMessage()
myMessage.content ="我是服务端发过来的消息"
myMessage.isConnect =true
val message = Message.obtain()
val bundle = Bundle()
bundle.putParcelable("message", myMessage)
message.data = bundle
replyMessenger?.send(message)
},3000)
}
}
}
第二步 在AndroidManifest.xml文件注册这个服务
<service android:name = ".RemoteService"
android:process=":remote">
</service>
第三步 创建一个实体类用于消息的收发MyMessage
需要注意的是这个实体类要实现Parcelable 接口。自己手动赋一些值。
import android.os.Parcel
import android.os.Parcelable
class MyMessage() : Parcelable {
override fun writeToParcel(dest: Parcel?, flags: Int) {
dest?.writeString(content)
dest?.writeByte(if(isConnect)1 else 0)
}
override fun describeContents(): Int {
return 0
}
var content: String =""
var isConnect: Boolean =false
constructor(parcel: Parcel) :this() {
content = parcel.readString()
isConnect = parcel.readByte() !=0.toByte()
}
companion object CREATOR : Parcelable.Creator {
override fun createFromParcel(parcel: Parcel): MyMessage {
return MyMessage(parcel)
}
override fun newArray(size: Int): Array {
return arrayOfNulls(size)
}
}
}
第四步编写测试的Activity
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.*
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.widget.Button
class MainActivity : AppCompatActivity() {
val handler = MyHandler(Looper.getMainLooper())
var messenger: Messenger? =null
var myMessenger: Messenger? = Messenger(handler)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById(R.id.send_message).setOnClickListener{
val myMessage = MyMessage()
myMessage.content ="我是客户端的消息"
val message = Message.obtain()
val bundle = Bundle()
bundle.putParcelable("message", myMessage)
message.data = bundle
message.replyTo =myMessenger
messenger?.send(message)
}
val intent = Intent(this, RemoteService::class.java)
bindService(intent,object : ServiceConnection {
override fun onServiceDisconnected(name: ComponentName?) {
}
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
messenger = Messenger(service)
}
}, Context.BIND_AUTO_CREATE)
}
inner class MyHandler(looper: Looper) : Handler(looper) {
override fun handleMessage(msg: Message?) {
super.handleMessage(msg)
val bundle = msg?.data
//这里是需要注意的地方。要设置一下classLoader否则回抛出异常
// android.os.BadParcelableException: ClassNotFoundException when unmarshalling: face.com.myaidltest.MyMessage2
bundle?.classLoader =MyMessage::class.java.classLoader
val myMessage = bundle?.getParcelable("message");
Log.d("RemoteService","客户端接收到的消息 == =" + myMessage?.content + myMessage?.isConnect)
}
}
}