Android communicates between two apps through Aidl

1、 Project introduction

[knowledge preparation]

① Android interface definition language (Aidl), which aims to realize cross process call. Process is the carrier of program execution in OS. A program corresponds to a process. Different processes refer to different programs. Aidl realizes the call between different programs.

② the main thread communicates with the sub thread using the handler. The handler can send messages in the sub thread and process messages in the main thread, so as to complete the communication between threads. Even if there are multiple threads, it is still a program.

③ different programs need to communicate through Aidl. There can be many communication modes, and Aidl is one of them. The result of the implementation is like its own program calling its own other methods, which feels like a program.

Business scenarios: for example, shopping app needs to be paid, shopping app is Taobao, and payment app is Alipay. Therefore, different programs are needed to communicate.

2、 First, introduce the communication between service and activity between an app

[project structure]

【MyService】

[prompt]

① create service

② if it is not created through the above method, be sure to register

[Code]

【layout_main】

Add button, click for easy calling

【MainActivity】

[effect]

Click to output the contents of the pay method in the service

3、 Service communication between two apps

[project structure]

[steps]

① Create myservice in apppayprovider

The code is the same as above

[registration]

I. when registering (Android: enabled = "true" Android: exported = "true"), set it to true, expose the service, and another app can access it

II. Add "< intent Filter >". Since it is not the same app, filter the intent through the intent filter and let another app start the service through action

② Mainactivity and layout_ Main remains unchanged during creation, but do not delete it, because the installer must provide a start page, otherwise an error will occur

③ Add Aidl in apppayprovider

[Code]

[prompt] the methods defined in the interface should be consistent with those in mybinder in service

④ After creating the Aidl and adding the method, Android studio needs to compile the Aidl, and will automatically generate the code of a binder subclass according to the Aidl specification.

⑤ Modify mybinder in myservice

[prompt] inherit ipay.stub. Before that, you must make project, otherwise there will be no association

⑥ Create apppayuser to operate on myservice in apppayprovider

【layout-main】

⑦ Copy Aidl in apppayprovider to apppayuser

[prompt] i. the package name should be the same. Copy it according to the directory location. Copy it directly in the folder through the following methods. "You can view the project structure here, and you can see that the package names are the same"

II. Make project is also required after copying

⑧【AppPayUser-MainActivity】

[installation]

Install apppayprovider before apppayuser.

[effect]

Transfer the view in run to apppayprovider and click the pay button in the simulator apppayuser to execute the content in the pay method in myservice in apppayprovider.

4、 Summary

[difference between cross app and the same app] ① opening a service across app means that the app providing the service needs to set an intent filter, and the app controlling the service needs to pass through. Set the action and package name with setaction and setpackage methods to start the service. The same app only needs to specify the service to start.

② Mybinder instances across apps need to be obtained through Aidl. The two applications define the same interface method, get the binder instance through the corresponding Aidl name. Stub.asinterface method, and then it is different from mybinder of the same app.

③ The use of mybinder objects across apps must catch exceptions, which are not required for the same app.

④ Many similar functions can be implemented according to the simple example above.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>