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

本次博客谈谈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. Win8 Metro(C#)数字图像处理--2.42图像光照效果算法

    原文:Win8 Metro(C#)数字图像处理--2.42图像光照效果算法  [函数名称] 图像光照效果  SunlightProcess(WriteableBitmap src,int X,in ...

  2. CSS计数器:counter

    最近的需求,明星字体销售排行榜中,需要对字体的销售情况进行排序. 在早期,只有ol和ul可以对子元素li进行排序:如果不使用这两个标签,就由前台开发去手动填写序号. 当然,在这个需求中,数据不是实时更 ...

  3. SignalR的简单使用(二)

    原文:SignalR的简单使用(二) 之前提到SignalR代理在网页,通过生成的Js来完成相关的功能.但我不禁想一个问题, 难到SignalR的服务端就能寄存在web端吗,通过访问网页能方式才能启动 ...

  4. 推荐一个第三方Qt库的集合 good

    https://inqlude.org/ Stable libraries | Development versions | Unreleased | Commercial | All attica ...

  5. Using 3D engines with Qt(可以整合到Qt里,不影响)

    A number of popular 3D engines can be integrated with Qt: Contents [hide]  1 Ogre 2 Irrlicht 3 OpenS ...

  6. Codility---PermCheck

    Task description A non-empty zero-indexed array A consisting of N integers is given. A permutation i ...

  7. Codility------CyclicRotation

    Task description A zero-indexed array A consisting of N integers is given. Rotation of the array mea ...

  8. 线性表List

    数组array是基本的数据结构,但它的功能有限,线性表list可以认为是扩展了功能的数组.可以自动调整大小.添加和删除元素不需要其他元素移位. 根据指针数量和指向的不同,线性表分为单向链表.双向链表和 ...

  9. [转]深入了解iPad上的MouseEvent

    iPad上没有鼠标,所以手指在触发触摸事件(TouchEvent)的时候,系统也会产生出模拟的鼠标事件(MouseEvent).      这对于普通网页的浏览需求而言,基本可以做到与PC端浏览器无明 ...

  10. Codeforces Round #563 (Div. 2)C

    C. Ehab and a Special Coloring Problem 题目链接:http://codeforces.com/contest/1174/problem/C 题目 You're g ...