AIDL学习笔记(一)----基本类型

AIDL学习笔记:
AIDL,Android接口定义语言,定义客户端和服务端的通信接口,所以AIDL文件在Service端和Client端都要声明
步骤流程:
service端:
1、新建AIDL文件夹
1-1、新建个package
1-2、新建AIDL文件
1-3、定义接口方法
2、定义service,创建接口内部类对象(IBinder),在service的onBinder()中返回IBinder对象

客户端:
1、建立AIDL文件夹
2、将service端的AIDL所在的包整个复制过来(在客户端声明通信接口)
3、显示方法启动service

    Intent intent = new Intent();
    intent.setComponent(new ComponentName("com.sjq.aidl_service","com.sjq.aidl_service.MyService"));
    bindService(intent,conn,BIND_AUTO_CREATE);

4、在绑定service回调中获取AIDL接口对象,调用其中定义的方法

代码如下:
service文件目录结构:
这里写图片描述`

AIDL:

// IMyAidlInterface.aidl
package com.sjq.aidl_service;

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

interface IMyAidlInterface {
   int add(int a,int b);
}

service:不要忘记在清单配置文件中注册

  <service
            android:name=".MySerice"
            android:exported="true"
            android:process=":remote">
package com.sjq.aidl_service;

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

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

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

    IMyAidlInterface.Stub binder = new IMyAidlInterface.Stub() {

        @Override
        public int add(int a, int b) throws RemoteException {
            Log.i("TAG", "我是来自服务端的add()");
            return a + b;
        }
    };
}

Client:文件目录
这里写图片描述

注意AIDL目录结构和名称要和service的完全相同,Service端的AIDL文件最好写在一个包里,直接粘贴复制过来

代码:

package com.sjq.aidl_client;

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

import com.sjq.aidl_service.IMyAidlInterface;

public class MainActivity extends AppCompatActivity {

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

    }

    public void doClick(View v){
        Intent intent = new Intent();
        //第一个参数是service所在的包名
        //第二个是包名+类名,完整的
        intent.setComponent(new ComponentName("com.sjq.aidl_service","com.sjq.aidl_service.MyService"));
        bindService(intent,conn,BIND_AUTO_CREATE);
    }

    ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            //转换成AIDL接口对象
            IMyAidlInterface  service = IMyAidlInterface.Stub.asInterface(iBinder);
            try {
                Log.i("TAG", "onServiceConnected: ------>"+service.add(1,3));
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };
}

这里写图片描述

下图是个人的一些整理:

这里写图片描述

猜你喜欢

转载自blog.csdn.net/lantiankongmo/article/details/51770162