好久未更新博客了。人都是这样,刚开始对某一样东东冲劲十足,时间一长,很难坚持下去了,我这博客也是。所以我要打破成规,继续更新。

本次博客谈谈adil的用法。aidl的全称叫什么来着忘了,不过不要紧,重点不是叫啥名,而是要领会这玩意儿是做啥的。扯远一点,我是越来越觉得,会具体的什么编程语言,会具体的什么api啥的,都是虚的,这些东西说白了,只是我们通向目标的一个工具。实现目标可以有很多工具实现,所以使用什么工具不重要,重要的是实现的方法、组织工具的思路、以及顺藤摸瓜快速解决问题的能力才是上乘的本事。

aidl是用于跨进程通信。可以看到,如果要达到跨进程通信,方法有多种,aidl只是其中的一种。

现在网上能搜到的关于aidl很多都是activity与service之间的通信,我记得官网上也是以这个为例,看来aidl应该适用于这种案例。再吐槽下,现在国内原创实在是太匮乏了。有一次我就在百度上搜一个技术点,搜了差不多前十多页,基本上只有一个原创,其余的都是复制。正所谓哀其不幸,怒其不争,整体ian吵嚷着国内it比上不上国外,那为啥不去多想几个为什么做一些原创?有时候我真的是恨恨的咬咬牙去谷歌了,还英文搜索,老外是相当多的原创。

1. 先定义aidl的接口,这个事什么时候都需要的。

package com.cn.sxp.aidl;
interface IAidlService {
String sayHello();
}

这个接口简单的一逼 啊,接下来就是搞一个服务了:

2. 服务代码:

package com.cn.sxp.aidl;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException; public class aidlServiceImp extends Service {
@Override
public IBinder onBind(Intent intent) {
return mBinder;
} private IAidlService.Stub mBinder = new IAidlService.Stub() {
public String sayHello() throws RemoteException {
return "hello world";
}
};
}

在服务中实现了这个接口,只是简单的返回一个字符串而已。

3. 再搞一个活动,链接这个服务,调用这个方法:

package com.cn.wx.client;

import com.cn.sxp.aidl.IAidlService;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException; public class AidlClientActivity extends Activity {
IAidlService mService = null; /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); beginBindService();
} private void beginBindService(){
Intent service = new Intent("com.cn.sxp.aidl.IAidlService");
bindService(service, mConnection, BIND_AUTO_CREATE);
} private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
mService = null;
} @Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
/**
* IAidlService.Stub类取一个实例对象
*/
mService = IAidlService.Stub.asInterface(service);
try {
mService.sayHello();
} catch (RemoteException re) {
re.printStackTrace();
}
}
};
}

完了,原创就这么简单,何必整天抄你抄他的。

android_aidl的更多相关文章

  1. Android 中 Service AIDL使用

         1.创建两个项目创建两个.aidl文件 2.在传递值的类里面创建Service并且返回接口: 服务返回值onBind public IBinder onBind(Intent intent) ...

  2. android基础---->AIDL服务的使用

    AIDL和其他的IDL类似,它允许你定义程序接口,以便客户端与服务器端通过IPC机制交互.在android上面,一个进程一般不能访问另外进程的内存.因此,Android平台将这些跨进程访问的对象分解成 ...

随机推荐

  1. 【Linux】samba服务

    samba是一个实现不同操作系统之间文件共享和打印机共享的一种SMB协议的免费软件. ①Samba软件包的安装 使用源安装,在终端中输入如下命令: #sudo apt-get install samb ...

  2. HTML5离线缓存攻击测试(二)

    经过昨天的测试,发现使用离线缓存的网站会被攻击.但是,不使用离线缓存的网站就真的不会受到这样的攻击么? 据我理解,按照标准当浏览器请求manifest文件时,若没有请求到,或者文件发生改变,应当不使用 ...

  3. Decision Tree

    Decision Tree builds classification or regression models in the form of a tree structure. It break d ...

  4. R3 HOOK OpenProcess 的问题

    unit HookAPI; //Download by http://www.codefans.net interface uses Windows, Classes; function Locate ...

  5. FireMonkey下的异形窗体拖动(需要使用FmxHandleToHwnd函数转化一下句柄)

    DelphiXE2 Firemoney FMX 的窗体不只是为windows的, 所以很多功能都没有了. 最常见的就是拖拽了 先看 VCL时代 一个经典拖动代码 ReleaseCapture(); S ...

  6. web的seo

    摘要:搜索引擎优化是一种具有很高技术性的活动,也是一种营销功能,必须将它作为营销活动处理.SEO从业者必须理解公司的服务.产品.总体业务战略.竞争形势.品牌.未来网站开发目标和相关的业务构成. SEO ...

  7. Dynamics 365中的事件框架与事件执行管道(Event execution pipeline)

    本文介绍了Microsoft Dynamics 365(以下简称D365)中的两个概念,事件框架(Event Framework)与事件执行管道(Event execution pipeline). ...

  8. kolla-ansible-----快速部署openstack

    基本环境 操作系统:CentOS Linux release 7.5.1804 (Core) 内核版本:3.10.0-862.el7.x86_64 docker版本:1.13.1 1.禁用宿主机的 L ...

  9. Zookeeper详解-安装(四)

    ZooKeeper服务器是用Java创建的,它在JVM上运行.你需要使用JDK 6或更高版本. 步骤1:验证Java安装 相信你已经在系统上安装了Java环境.现在只需使用以下命令验证它. $ jav ...

  10. 【LEETCODE】32、LeetCode的第35题,查找插入的位置

    凉凉,看来想做好一个题还不容易啊... 有点难受... 1.看看题目吧 Given a sorted array and a target value, return the index if the ...