freeBuf
主站

分类

云安全 AI安全 开发安全 终端安全 数据安全 Web安全 基础安全 企业安全 关基安全 移动安全 系统安全 其他安全

特色

热点 工具 漏洞 人物志 活动 安全招聘 攻防演练 政策法规

点我创作

试试在FreeBuf发布您的第一篇文章 让安全圈留下您的足迹
我知道了

官方公众号企业安全新浪微博

FreeBuf.COM网络安全行业门户,每日发布专业的安全资讯、技术剖析。

FreeBuf+小程序

FreeBuf+小程序

Android开发笔记 ——(5)AIDL
chenhh 2021-12-23 17:21:58 21578
所属地 广东省

AIDL全称:Android Interface definition language
作用:进程间的通信接口,相当于A进程(客户端)可以通过AIDL接口去调用另一个进程B(服务端)的方法

使用:
1、创建aidl文件
2、自动生成的java文件
3、使用aidl

AIDL和contentprovide的区别?
用途不一样, aidl调用服务的相关方法,强调的是方法。contentprovide强调的是数据之间的共享。

B进程这边:
创建aidl文件:
image.png

定义远程接口:

// IMyAidlInterface.aidl
package com.example.servicedemo;

// 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.
     */
    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
            double aDouble, String aString);

     //定义自己所需要的方法:显示当前服务的进度
     void showProgress();
}
//绑定
    //IBinder:在android中用于远程操作对象的一个基本接口
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        Log.e("TAG","服务绑定了");
        //Binder
        return new IMyAidlInterface.Stub(){
            @Override
            public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

            }

            @Override
            public void showProgress() throws RemoteException {
                Log.e("TAG","当前进度是" + i);
            }
        };
    }

A进程这边复制B进程生成的AIDL代码,然后就可以使用B进程的AIDL接口中的方法了。
使用接口:

package com.example.aidldemo;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
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 com.example.servicedemo.IMyAidlInterface;

public class MainActivity extends AppCompatActivity {

    ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            IMyAidlInterface imai = IMyAidlInterface.Stub.asInterface(iBinder);
            try {
                imai.showProgress();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

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

    public void operate(View v){
        switch(v.getId()){
            case R.id.start:
                //远程启动服务
                Intent it = new Intent();
                it.setAction("com.imooc.myservice");
                it.setPackage("com.example.servicedemo");
                startService(it);
                break;
            case R.id.stop:
                Intent it2 = new Intent();
                it2.setAction("com.imooc.myservice");
                it2.setPackage("com.example.servicedemo");
                stopService(it2);
                break;
            case R.id.bind:
                Intent it3 = new Intent();
                it3.setAction("com.imooc.myservice");
                it3.setPackage("com.example.servicedemo");
                bindService(it3,conn,BIND_AUTO_CREATE);
                break;
            case R.id.unbind:
                unbindService(conn);
                break;

        }
    }
}
# Android开发
本文为 chenhh 独立观点,未经授权禁止转载。
如需授权、对文章有疑问或需删除稿件,请联系 FreeBuf 客服小蜜蜂(微信:freebee1024)
被以下专辑收录,发现更多精彩内容
+ 收入我的专辑
+ 加入我的收藏
chenhh LV.5
这家伙太懒了,还未填写个人描述!
  • 17 文章数
  • 11 关注者
Android开发笔记 ——(11)流行框架
2021-12-27
Android开发笔记 ——(10)高级控件
2021-12-27
Android开发笔记 ——(9)AsyncTask异步任务
2021-12-24