In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "how to use the Binder class". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Aidl
Server defines interface
Define the aidl file and put it in the src directory
Implement the inner class stub after compiling aidl
The Android SDK tool generates an interface using the Java programming language based on your .aidl file. This interface has an internal abstract class called Stub that extends the Binder class and implements methods in the AIDL interface. You must extend the Stub class and implement these methods.
Expose the interface to the client
Implement Service and override onBind () to return the implementation of the Stub class.
Client use
Use bindService ()
The src directory contains the aidl file, which provides the client with permission to use adil
Note: the invocation is synchronous. If the service is time-consuming and needs to be invoked by child threads, thread safety needs to be considered.
Transitive class
Currently, only basic type data can be passed. Passing object types requires the model class to implement Parcelable.
The caller always catches DeadObjectException exceptions
DeadObjectException
Code example / / IRemoteService.aidlpackage com.example.android// Declare any non-default types here with import statements/** Example service interface * / internal interface IRemoteService {/ * * Request the process ID of this service, to do evil things with it. * / val pid:Int / * * Demonstrates some basic types that you can use as parameters * and return values in AIDL. * / fun basicTypes (anInt:Int, aLong:Long, aBoolean:Boolean, aFloat:Float, aDouble:Double) AString:String)} generate IRemoteService extends Binder implements IMyService {class Stub extends android.os.Binder implements IMyService {/ / client takes Binder to get a Proxy proxy object private static class Proxy implements IMyService {}} / / expose the interface class RemoteService: Service () {override fun onCreate () { Super.onCreate ()} override fun onBind (intent: Intent): IBinder {/ / Return the interface return binder} / / implementation stub privateval binder = object: IRemoteService.Stub () {override fun getPid (): Int {return Process.myPid ()} override fun basicTypes (anInt: Int ALong: Long, aBoolean: Boolean, aFloat: Float, aDouble: Double, aString: String) {/ / Does nothing}} var iRemoteService: IRemoteService? = nullval mConnection = object: ServiceConnection {/ / Called when the connection with the service is established override fun onServiceConnected (className: ComponentName Service: IBinder) {/ / Following the example above for an AIDL interface, / / this gets an instance of the IRemoteInterface, which we can use to call on the service iRemoteService = IRemoteService.Stub.asInterface (service)} / / Called when the connection with the service disconnects unexpectedly override fun onServiceDisconnected (className: ComponentName) {Log.e (TAG, "Service has unexpectedly disconnected") iRemoteService = null}} bind services
1. Extend the binder class and write it in a conventional service way. Non-boast process, used for background service processing 2, Messenger processing cross-process. The feature is that there is no need to think about multithreading safety, which is that messenger maintained through handler creates queues for all requests. Using simple 3, aidl, you need to consider the problem of multithreading across processes. The design is relatively complex
/ / messenger service creation / * * Command to the service to display a message * / private const val MSG_SAY_HELLO = 1class MessengerService: Service () {/ * Target we publish for clients to send messages to IncomingHandler. * / private lateinit var mMessenger: Messenger / * * Handler of incoming messages from clients. * / internal class IncomingHandler (context: Context, privateval applicationContext: Context = context.applicationContext): Handler () {override fun handleMessage (msg: Message) {when (msg.what) {MSG_SAY_HELLO-> Toast.makeText (applicationContext, "hello!" Toast.LENGTH_SHORT). Show () else-> super.handleMessage (msg)} / * * When binding to the service, we return an interface to our messenger * for sending messages to the service. * / override fun onBind (intent: Intent): IBinder? {Toast.makeText (applicationContext, "binding", Toast.LENGTH_SHORT). Show () mMessenger = Messenger (IncomingHandler (this)) return mMessenger.binder}} that's all for "how to use Binder classes". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.