Aidl Use simple communication _aidl

Source: Internet
Author: User
Tags stub
Record bit aidl simple case

I'm a little busy. It's time to stick to one blog a week. 20170809 Record your own drip
(No forwarding without my permission) directory

The reason for writing this article is: There is a need: to get an interface to let the third party app call it open, turn it on, shut it down, shut it down, and forget it. I'm afraid I later forgot the source code compiled and common people write Adil different!!!!!!!!!!! Body part:

Since it is to provide the interface to go out with Aidl is also a good choice

Recently in charge of Systemui This module has a need to control whether the virtual key is clickable/visible; Virtual button is visible everyone should know how to do (not Baidu OH) to control whether you can click on this need to modify the source code of the Systemui. Since it is systemui, then need to use the source code to compile (anti-human decompile the side to go!). You need to call the System app-level method or some interfaces can do this. (Reflective, on one side) need to learn aidl and then look:

Interrupted: Here does not have to aidl the specialized terminology to carry on an explanation to need to understand (goes out to turn right on Baidu)

The direct point is: two of apps communicate between the bridge to provide the interface to the third party

[If you are also in the system module also need to provide interface so you are very lucky]

Create a project (Aidl service side)

1. Use Androidstudio to create an engineering project named with the project for example: Systemuitest

2. Open Systemuitest's engineering path to remove all files in the main directory
For example:/androidstudioprojects/systemui/app/src/main/delete all

3. Copy the responsible module source code to the main directory, and modify the SRC filename to Java (the end result is as follows:)
Set up Aidl with Androidstudio Open Systemuitest Project Error Please ignore because we compile using the source code, using Androidstudio just to facilitate the generation of stub class (temporarily do not use the source code to do) in the project to create aidl files:
– Defines the package name for a good aidl
– Right-click package name new>aidl>aidl file creation complete Aidl open. aidl file comment out Basictypes () method Add the interface that you need to provide out, L for example: Clicknavigationbarhome (Boolean Click
The Basictypes () method only describes the types of parameters that can communicate
And then sync. The project will generate. java files in the following directory

1. All the above is done just to get Imyaidlinterface.java. Next, copy the Imyaidlinterface.java file to our project. Note that you need to create the package name to match the package name of the file (please ignore the error) As for the Aidl folder in Androidstudio is provided to the use of third parties here you can come to an ending.

followed by the concrete implementation of our interface to create a service in the project, remember in the Androidmanifest statement OH
Then look at the implementation of the server-side interface: You can refer to the code:

public class Yyynavigationbarservice extends service {
    //Services action
    final String action = "com.systemui.adb";
    @Override public
    IBinder onbind (Intent Intent) {  
        if (intent.getaction (). Equals (Action))
        {
            return Mibinder;
        }
        return null;
    }

    @Override Public
    Boolean onunbind (Intent Intent) {return
        super.onunbind (Intent);
    }

    @Override public
    void OnDestroy () {
        Super.ondestroy ();
    }

    @Override public
    void OnCreate () {
        super.oncreate ();
    }

    Private final IBinder Mibinder = new Imyaidlinterface.stub () 
    {

        @Override public
        Void Clicknavigationbarhome (Boolean click) throws RemoteException {
         /***
            * Specific implementation of their own wrote here is the third party to invoke the interface after the server received processing
            * /
        }

    };  
}
Finally open Systemuitest Engineering folder main directory to change the Java file name to SRC in addition to Aidl folder, copy all files, replace the source code on the Systemui project build systemui.apk and then push to the system

To this service side to complete

Customer Service side (third-party app)

If you are not using this interface, then congratulations you can work, before work need to provide the interface to the third party, directly to the Aidl file of the. aidl file to him on the line.

Then the egg has to be validated. So it's easy. Third party if it is Androidstudio engineering is not necessary source code compiled in the project to create a aidl note the package name needs to be consistent with the package name in the interface file. aidl file and replace the. aidl file provided above.
The following binding is followed by the remote Service invocation interface, for example:

public class Navigationbarmanager {final String action_add = "COM.SYSTEMUI.ADB";

    Imyaidlinterface mservice = null; 
        public void Bindservice {mcontext = context;
    Context.bindservice (New Intent (Action), serviceconnection, context.bind_auto_create);

    Context mcontext = null;
        public void Unbindservice () {if (Mcontext!= null) {Mcontext.unbindservice (serviceconnection);

        } public int Click (Boolean click) {if (Mservice = null) return 0;
        try {return mservice.clicknavigationbarhome (click);
        catch (Exception e) {return 0; } private Serviceconnection serviceconnection = new Serviceconnection () {@Override public vo
        ID onservicedisconnected (componentname name) {mservice = null; @Override public void onserviceconnected (ComponentnaMe name, IBinder service) {mservice = IMyAidlInterface.Stub.asInterface (service);
}
    }; }
When a third party invokes the interface, it first goes to bind the service and then invokes the click (Boolean click) with the Navigationbarmanager object. I have to pee.

Additional:
What if your third party apk also need to compile in the source code?
-Copy the contents of the Aidl folder above to src/folder
-Add a declaration to the Mk file: for example:

-Implement the interface or imitate the Navigationbarmanager class can

Write this one afternoon ... a little disgusting.

There are questions welcome message:

Rich Welcome to Play Rewards:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.