1、为什么要有AIDL?

不管学什么东西,最先得弄明确为什么要有这个东西。不要说存在即是合理。存在肯定合理,可是你还是没有明确。

对于AIDL有一些人的浅显概念就是,AIDL能够跨进程訪问其它应用程序,和其它应用程序通讯,那我告诉你。非常多技术都能够訪问,如广播(应用A在AndroidManifest.xml中注冊指定Action的广播)应用B发送指定Action的广播,A就能收到信息,这样也能看成不同应用之间完毕了通讯(可是这样的通讯是单向的)。还如ContentProvider,通过URI接口暴露数据给其它应用訪问;可是这样的都算不上是应用之间的通讯。

可能最让人迷惑的是Android推出来了Messager,它就是完毕应用之间的通讯的。那么为什么还要有AIDL呢。官方文档介绍AIDL中有这么一句话:

Note: Using AIDL is necessary only if you allow clients from different applications to access your service for IPC and want to handle multithreading in your service. If you do not need to perform concurrent IPC across different applications, you should create your interface by implementing a Binder or, if you want to perform IPC, but do not need to handle multithreading, implement your interface using a Messenger. Regardless, be sure that you understand Bound Services before implementing an AIDL.

第一句最重要,“仅仅有当你同意来自不同的client訪问你的服务而且须要处理多线程问题时你才必须使用AIDL”。其它情况下你都能够选择其它方法。如使用Messager,也能跨进程通讯。

可见AIDL是处理多线程、多client并发訪问的。而Messager是单线程处理。还是官方文档说的明确,一句话就能够理解为什么要有AIDL。那么是不是这样的写个AIDL试试。

2、AIDL使用

第一、定义AIDL文件
// IRemoteService.aidl
package com.example.android; // Declare any non-default types here with import statements /** Example service interface */
interface IRemoteService {
/** Request the process ID of this service, to do evil things with it. */
int getPid(); /** 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);
}

这段代码也是官方文档的。

命名为IRemoteService.aidl。放在com.example.android包下(这个能够任意),保存后Android编译器会在gen文件夹下自己主动生成IRemoteService.java文件

第二、定义我们的服务,DDService.java,而且须要在AndroidManifest.xml中注冊,并加入“duanqing.test.aidl” 的ACTION
package com.example.service;

import com.example.android.IRemoteService;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.Process; public class DDService extends Service {
@Override
public void onCreate() {
super.onCreate();
System.out.println("DDService onCreate........" + "Thread: " + Thread.currentThread().getName());
}
@Override
public IBinder onBind(Intent arg0) {
System.out.println("DDService onBind");
return mBinder;
} private final IRemoteService.Stub mBinder = new IRemoteService.Stub() {
public int getPid(){
System.out.println("Thread: " + Thread.currentThread().getName());
System.out.println("DDService getPid ");
return Process.myPid();
}
public void basicTypes(int anInt, long aLong, boolean aBoolean,
float aFloat, double aDouble, String aString) {
System.out.println("Thread: " + Thread.currentThread().getName());
System.out.println("basicTypes aDouble: " + aDouble +" anInt: " + anInt+" aBoolean " + aBoolean+" aString " + aString);
}
}; }

这样我们的服务端就完毕了。把服务端执行到模拟器(或者手机上)。等一会能够看一下信息打印,重点看“线程名”


第三、实现client測试代码
新建还有一个project,相同须要加入AIDL协议文件(这是一个标准的协议文件。定义对外服务),这里我列出来我的測试代码:
package com.example.aidlclient;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Process;
import android.os.RemoteException;
import android.view.View; import com.example.android.IRemoteService; public class MainActivity extends Activity {
private IRemoteService remoteService;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} ServiceConnection conn = new ServiceConnection() { @Override
public void onServiceDisconnected(ComponentName name) {
} @Override
public void onServiceConnected(ComponentName name, IBinder service) {
remoteService = IRemoteService.Stub.asInterface(service);
try {
int pid = remoteService.getPid();
int currentPid = Process.myPid();
System.out.println("currentPID: " + currentPid +" remotePID: " + pid);
remoteService.basicTypes(12, 1223, true, 12.2f, 12.3, "我们的爱,我明确");
} catch (RemoteException e) {
e.printStackTrace();
}
System.out.println("bind success! " + remoteService.toString());
}
}; /**
* 监听按钮点击
* @param view
*/
public void buttonClick(View view) {
System.out.println("begin bindService");
Intent intent = new Intent("duanqing.test.aidl");
bindService(intent, conn, Context.BIND_AUTO_CREATE);
} @Override
protected void onDestroy() {
super.onDestroy();
unbindService(conn);
}
}

4、运行

点击clientbutton,运行。看信息打印:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2FuZ2R1YW5xaW5nNTk0NQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">


看服务端打印,DDService onCreate..........Thread: main,主线程,当client调用服务端getPid方法时,服务端是在Thread: Binder2中运行,当client调用服务端basicType方法时,服务端是在Thread:Binder1中运行

彻底明确Android中AIDL及其使用的更多相关文章

  1. Android中AIDL的理解与使用(二)——跨应用绑定Service并通信

    跨应用绑定Service并通信: 1.(StartServiceFromAnotherApp)AIDL文件中新增接口: void setData(String data); AppService文件中 ...

  2. Android中AIDL的理解与使用(一)——跨应用启动/绑定Service

    AIDL(Android Interface Definition Language)--安卓接口定义语言 一.startService/stopService 1.同一个应用程序启动Service: ...

  3. Android中AIDL通信机制分析

    一.背景 ·1.AIDL出现的原因 在android系统中,每一个程序都是运行在自己的进程中,进程之间无法进行通讯,为了在Android平台,一个进程通常不能访问另一个进程的内存空间,所以要想对话,需 ...

  4. (七)Android中AIDL的应用与理解

    一.跨应用启动Service Intent serviceIntent=new Intent();serviceIntent.setComponent(new ComponentName(" ...

  5. Android 中AIDL的使用与理解

    AIDL的使用: 最常见的aidl的使用就是Service的跨进程通信了,那么我们就写一个Activity和Service的跨进程通信吧. 首先,我们就在AS里面新建一个aidl文件(ps:现在AS建 ...

  6. android中的AIDL进程间通信

    关于IPC应该不用多介绍了,Android系统中的进程之间不能共享内存,那么如果两个不同的应用程序之间需要通讯怎么办呢?比如公司的一个项目要更新,产品的需求是依附于当前项目开发一个插件,但是呢这个插件 ...

  7. Android之——AIDL深入

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47071927 在上一篇博文<Android之--AIDL小结>中,我们 ...

  8. Android进程间通信-AIDL实现原理

    Android进程间通信基于Proxy(代理)与Stub(桩或存根)的设计模式(如图1-1所示).其中,Proxy将特殊性接口转换成通用性接口,Stub将通用性接口转换成特殊性接口,二者之间的数据转换 ...

  9. Android 中的AIDL,Parcelable和远程服务

    Android 中的AIDL,Parcelable和远程服务      早期在学习期间便接触到AIDL,当时对此的运用也是一撇而过.只到近日在项目中接触到AIDL,才开始仔细深入.AIDL的作用    ...

随机推荐

  1. xmu 1125 越野车大赛(公式推导&amp;&amp;三分)(中等)

    xmu 1125.越野车大赛 Description TheBeet正在參加一场越野车大赛.比赛的场地如右图:共分三块.每一块地面的长宽均为N与M,但地表情况不同.越野车在这段路面上的最快速度也不同. ...

  2. PotPlayer 如何设置多屏幕全屏播放

      如何在播放器中,设置扩展播放模式? 全屏设置/主全屏显示设备:Display2   如何使视频播放时,没有黑边并且全屏充满? 高宽比/处理方式:保持全屏宽高比   如果取消掉视频上方的文字提示信息 ...

  3. 执行次数最多的sql语句

    执行次数最多的sql语句 * FROM ( creation_time N'语句编译时间' ,DB_NAME(st.dbid) AS dbname ,OBJECT_NAME(st.objectid) ...

  4. TensorFlow编译androiddemo

    首先是把tensorflow克隆到本地一份. git clone --recurse-submodules https://github.com/tensorflow/tensorflow.git 既 ...

  5. 转:简单通用的一则makefile .

    在linux下面下写程序少不了写makefile,如果每个文件都按部就班的详细的写编译脚本,效率势必低下:makefile提供了自动化变量.模式规则等,稍加利用可以提高写makefile的效率.下面列 ...

  6. opensips编译安装时可能遇到的问题

    错误一: ERROR: could not load the script in /usr/local//lib64/opensips/opensipsctl/opensipsdbctl.pgsql ...

  7. php getallheaders使用注意事项

    This function is an alias for apache_request_headers(). Please read the apache_request_headers() doc ...

  8. 批量修改图像的大小 Python PIL

    #-*-coding:utf-8-*- import os import os.path from PIL import Image import time def ResizeImage(filei ...

  9. atitit.按钮光标滑过高亮切换以及其他动态效果的实现css html js --attilax总结

    atitit.按钮光标滑过高亮切换以及其他动态效果的实现css html  js --attilax总结 4. 鼠标越过动态图片切换实现 1 4.1. 优先模式::css模式... 1 4.2. 其次 ...

  10. Atitit.js javascript异常处理机制与java异常的转换.js exception process Voae

    Atitit.js javascript异常处理机制与java异常的转换.js exception processVoae 1. 1. javascript异常处理机制 1 2. 2. Web前后台异 ...