Android: A simple adil example

Keywords: Android

Adil, cross-process communication, a simple example, to understand adil.

Create a project with arbitrary name. Since we want to cross-process, we create a service that runs in a separate thread according to the onprocess method.

 <service
            android:process=":service_new"
            android:name=".RemoteService"
            android:enabled="true"
            android:exported="true">
        </service>

The name here is RemoteService, which is also the most primitive method to implement by returning an adil interface instance through this binding method.

First, let's create an adil file

The default name is IMyAidlInterface, which is created as shown in the figure.

There is a default method, which is the way we want to operate.
Modify it to the way we want it. Simply complete a+b, we change it to

package com.example.jb.aidl;

// Declare any non-default types here with import statements

interface IMyAidlInterface {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
   int add(int num,int num2);
}

The interface is implemented, so we need to interact in the code and instantiate it in the service.

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

public class RemoteService extends Service {
    public RemoteService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return new IBinder();    //Return instance after binding
    }

    private class IBinder extends IMyAidlInterface.Stub {        //Implementing the method by inheriting IMyAidlInterface.Stub

        @Override
        public int add(int num, int num1) throws RemoteException {
            return num + num1;                    
        }
    };
}

Now that we have implemented the method in service, our method is to get the instance in activity and add and subtract at the same time.

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private String TAG="MainActivity";
    private Button toAdd;

    IMyAidlInterface iMyAidlInterface;

    private ServiceConnection conn = new ServiceConnection() {
        //When binding services
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            //Received remote services
            Log.e(TAG, "Binding success");
            iMyAidlInterface = IMyAidlInterface.Stub.asInterface(iBinder);

        }

        // Called when the service is disconnected
        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Log.e(TAG, "Binding loss");
            //Recycling resources
            iMyAidlInterface = null;

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

        bindServce();
    }

    private void initView() {

        toAdd = (Button) findViewById(R.id.toAdd);
        toAdd.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        try {
            //Calling remote services
            int valueRes = iMyAidlInterface.add(1, 2);
            Log.e(TAG, valueRes+"");
        } catch (RemoteException e) {
            e.printStackTrace();
        }


    }

    private void bindServce() {
        //After getting to the server, the bound service must be displayed after 5.0
        Intent intent = new Intent(this,RemoteService.class);
        bindService(intent, conn, BIND_AUTO_CREATE);
        Log.e(TAG, "bindServce: bind on end");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(conn);
    }

}

The layout code is very simple, just a button. Every click on the button will achieve 1 + 2 operation, and print out in the console.

A simple model implements the communication between service and activity in different processes.

You may ask, is not it binding service, what process, I also tried, not through the aidl file directly binding service, the program will collapse directly, after all, they do not belong to the same process, conventional bindservice is certainly not feasible.

Any questions above are welcome to be corrected.

Posted by nogeekyet on Thu, 23 May 2019 15:15:10 -0700