Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction and use of Binder Mechanism in Android

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "introduction and use of Binder mechanism in Android". In the operation of actual cases, many people will encounter such a dilemma, 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!

Binder is used for communication between android processes. Client programs and system services, client processes, all communicate between processes through binder.

1. Client programs and system services

The entrance of communication between client and system service: cotext.getSystemService (String name)

Android.app.ContextImpl:

@ Override

Public Object getSystemService (String name) {

Return SystemServiceRegistry.getSystemService (this, name)

}

Android.app.SystemServiceRegistry:

Static code block registration system service:

Static {

RegisterService (Context.ACCESSIBILITY_SERVICE, AccessibilityManager.class

New CachedServiceFetcher () {

@ Override

Public AccessibilityManager createService (ContextImpl ctx) {

Return AccessibilityManager.getInstance (ctx)

})

RegisterService (Context.CAPTIONING_SERVICE, CaptioningManager.class

New CachedServiceFetcher () {

@ Override

Public CaptioningManager createService (ContextImpl ctx) {

Return new CaptioningManager (ctx)

})

RegisterService (Context.ACCOUNT_SERVICE, AccountManager.class

New CachedServiceFetcher () {

@ Override

Public AccountManager createService (ContextImpl ctx) {

IBinder b = ServiceManager.getService (Context.ACCOUNT_SERVICE)

IAccountManager service = IAccountManager.Stub.asInterface (b)

Return new AccountManager (ctx, service)

})

....

ServiceManager 、 ServiceManagerNative:

ServiceManager is a utility class for managing system services.

SServiceManager = ServiceManagerNative

.asInterface (Binder.allowBlocking (BinderInternal.getContextObject ()

GetService method: use the ServiceManagerProxy class to obtain the binder object mRemote of other services in the binder driver by means of inter-process communication.

Xxx.Stub.asInterface (binder):

The binder object obtained by getService instantiates the Proxy object of the corresponding service and returns it to the client for use by the client.

Return to the system service:

/ * *

* Gets a system service from a given context.

, /

Public static Object getSystemService (ContextImpl ctx, String name) {

ServiceFetcher fetcher = SYSTEM_SERVICE_FETCHERS.get (name)

Return fetcher! = null? Fetcher.getService (ctx): null

}

two。 Between clients

B application process calls An application process

B bindservice requests AmS to launch the service of B application. After starting service successfully, it will return a (mRemote in the binder driver) like AmS. Binder,AmS will call the ApplicatonThread object in the ActivityThread class with this binder as a parameter. The conn interface is then called back in ApplicationThread. Finally, the B process can use the binder to invoke the functions provided by the An application.

This is the end of the introduction and use of Binder mechanism in Android. Thank you for your 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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report