Pro Android with Kotlin
Download 5.42 Mb. Pdf ko'rish
|
@de android telegram Pro Android with Kotlin Developing Modern Mobile
38
CHAPTER 4: Services private val myConn = object : ServiceConnection { override fun onServiceConnected(className: ComponentName, service: IBinder) { remoteSrvc = Messenger(service) backData = Messenger( InHandler(this@MainActivity)) // establish backchannel val msg0 = Message.obtain() msg0.replyTo = backData remoteSrvc?.send(msg0) // handle forward (client -> service) // connectivity... } override fun onServiceDisconnected(clazz: ComponentName) { remoteSrvc = null } } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // bind to the service, use ID from the manifest! val intent = Intent(" .START_SERVICE") intent.setPackage(" ") val flags = Context.BIND_AUTO_CREATE bindService(intent, myConn, flags) } } The service can then use this message, extract the replyTo attribute, and use it to send messages to the service client. internal class IncomingHandler(val ctx: Context) : Handler() { override fun handleMessage(msg: Message) { val s = msg.data.getString("MyString") val repl = msg.replyTo Toast.makeText(ctx, s, Toast.LENGTH_SHORT).show() Log.e("IncomingHandler", "!!! " + s) Log.e("IncomingHandler", "!!! replyTo = " + repl) // If not null, we can now use the 'repl' to send // messages to the client. Of course we can save // it elsewhere and use it later as well if(repl != null) { val thr = Thread( object : Runnable { 39 CHAPTER 4: Services override fun run() { Thread.sleep(3000) val msg = Message.obtain() val bundle = Bundle() bundle.putString("MyString", "A reply message to be sent") msg.data = bundle repl?.send(msg) } } ) thr.start() } } } The other two methods, using a broadcast message or a ResultReceiver class, get handled in Chapters 5 and 12 . Service Subclasses Up to now we were always using android.app.Service as a base class for services we described. There are other classes supplied by Android that are usable as base classes, though, with different semantics. For Android 8.0, there are no less than 20 service classes or base classes you can use. You can see them all in the Android API documentation in the “Known Direct Subclasses” section. Note At the time of writing this book, you can find this documentation at https://developer. android.com/reference/android/app/Service.html . The most important service classes are the following three: android.app.Service: This is the one we’ve been using so far. This is the most basic service class. Unless you use multithreading inside the service class or the service is explicitly configured to execute in another process, the service will be running inside the service caller’s main thread. If this is the GUI thread and you don’t expect the service invocation to run really fast, it is strongly recommended you send service activities to a background thread. android.app.IntentService: While a service by design does not naturally handle incoming start requests simultaneously to the main thread, an IntentService uses a dedicated worker thread to receive multiple start messages. Still, it uses just one thread to work with start requests, so they get executed one after the other. IntentService classes take care of correctly stopping services, so you don’t need to care about this yourself. You have to provide the service’s work to be done for each start request inside an overwritten |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling