AIDL调用指南
近期有需求要实现两个apk之间的通信,想到用AIDL来实现,现写一个demo学习下AIDL怎样使用。
这里我要实现一个apk(client端)调用还有一个apk(server端)的方法.
先实现server端。代码结构例如以下
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZmVuZ3FpYW95ZWJvMjAwOA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
AIDL文件内容例如以下:
package com.example.testaidl;
interface MyInterface {
void testMethod();
}
MainActivity.java
package com.example.testaidlserver; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService(new Intent(this, MyService.class));
} }
MyService.java
package com.example.testaidlserver; import com.example.testaidl.MyInterface; 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 { @Override
public IBinder onBind(Intent intent) {
Log.i("MyService", "service onBind....");
return new ImplAIDLService();
} private class ImplAIDLService extends MyInterface.Stub { @Override
public void testMethod() throws RemoteException {
Log.i("MyService", "testMode invoked");
}
}
}
Manifest中加入MyService的注冊
<service android:name="com.example.testaidlserver.MyService">
<intent-filter>
<action android:name="com.example.testaidlserver.MyService"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</service>
以下是Client端的
aidl文件和server端的要一样
package com.example.testaidl;
interface MyInterface {
void testMethod();
}
MainAcitvity的功能是,绑定server端的service。调用server端的方法
package com.example.testaidlclient; import com.example.testaidl.MyInterface; 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.RemoteException;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity implements OnClickListener {
private boolean mIsBinded = false;
private MyInterface mInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn_bind = (Button)findViewById(R.id.btn_bind);
btn_bind.setOnClickListener(this);
Button btn_invoke = (Button)findViewById(R.id.btn_invoke);
btn_invoke.setOnClickListener(this);
} @Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.btn_bind:
if(mIsBinded) {
unbindService(con);
}else {
bindService(new Intent("com.example.testaidlserver.MyService"), con, Context.BIND_AUTO_CREATE);
}
break;
case R.id.btn_invoke:
try {
mInterface.testMethod();
} catch (RemoteException e) {
e.printStackTrace();
}
break;
}
} ServiceConnection con = new ServiceConnection() { @Override
public void onServiceDisconnected(ComponentName name) {
mIsBinded = false;
mInterface = null;
} @Override
public void onServiceConnected(ComponentName name, IBinder service) {
mIsBinded = true;
mInterface = MyInterface.Stub.asInterface(service);
}
}; }
执行结果:
AIDL调用指南的更多相关文章
- Android 使用AIDL调用外部服务
好处:多个应用程序之间建立共同的服务机制,通过AIDL在不同应用程序之间达到数据的共享和数据相互操作, 本文包括: 1 .创建AIDL 服务端.2 .创建AIDL 客户端. 3.客户端调用服务端提供的 ...
- Android -- service的开启方式, start开启和绑定开启服务,调用服务的的方法, aidl调用远程服务
1. 概述 bindService() 绑定服务 可以得到服务的代理人对象,间接调用服务里面的方法. 绑定服务: 间接调用服务里面的方法. 如果调用者activity被销毁了, ...
- 使用AIDL调用远程服务设置系统时间
在实际工作中,经常遇到客户需要用代码设置系统时间的需求,但是Android非系统应用是无法设置系统时间的.于是,我设计了一个使用系统签名的时间设置服务,客户通过bind调用服务里的方法就能达到设置时间 ...
- 腾讯AI开放平台的接口调用指南
最近无意发现腾讯AI开放平台上提供了大量好玩的人工智能云服务,而且是完全免费的.只需要用QQ号登录即可.这么好的东西,作为一个程序员,当然要试试了! 从上图可以看出腾讯AI开放平台提供的人工智能服务主 ...
- Android Studio实现Service AIDL
Android Studio实现Service AIDL [日期:2015-01-02] 来源:Linux社区 作者:teenyboy [字体:大 中 小] 今天要开发过程中要用到AID ...
- 大仙说道之Android studio实现Service AIDL
今天要开发过程中要用到AIDL的调用,之前用的eclipse有大量教程,用起来很方便,现在刚换了Android studio,不可否认studio真的很强大,只是很多功能还需要摸索. AIDL(And ...
- Android - AIDL 使用
AIDL(Android Interface Definition Language) 程序员可以利用AIDL自定义编程接口,在客户端和服务端之间实现进程间通信(IPC).在Android平台上,一个 ...
- Binder or AIDL的最简单实践
1.前言: 在Android开发中多进程的合理使用+进程间通信的IPC是一个比较难的点.特别是Android特有的Binder机制,非常复杂,对于应用层开发的初级开发工程师强求深入理解Binder机制 ...
- 让我们一起学习如何使用AIDL,它其实并不难(Android)
前言 该篇文件讲述的是AIDL最基本的使用(创建.调用),关于对于AIDL更深的认识,在后续的随笔中,会持续与大家分享并探讨. 正文 AIDL的定义(什么是AIDL?) AIDL的应用场景(AIDL可 ...
随机推荐
- 5分钟在Mac上从0配置安装laravel5.5
1.安装包管理工具homebrew ,相当于ubuntu的apt-get 在iTerm命令行输入: /usr/bin/ruby -e "$(curl -fsSL https://raw.gi ...
- 基于opencv的摄像头的标定
四个坐标系分别为:世界坐标系(Ow),摄像机坐标系(Oc),图像物理坐标系(O1,单位mm),图像像素坐标系(O,位于视野平面的左上角,单位pix). 空间某点P到其像点p的坐标转换过程主要是通过这四 ...
- Codeforces 853C - Boredom
853C - Boredom 题意 给出一个矩阵,每行每列有且仅有一个点.每次询问一个子矩形,问这些点构成的矩形有多少个与给定的矩形相交(两个处于对角线上的点可以组成矩形). 分析 考虑矩形周围 8 ...
- ZOJ 2112 Dynamic Rankings (动态第 K 大)(树状数组套主席树)
Dynamic Rankings Time Limit: 10 Seconds Memory Limit: 32768 KB The Company Dynamic Rankings has ...
- 51nod 子序列的个数(动态规划)
子序列的个数 给定一个正整数序列,序列中元素的个数和元素值大小都不超过105, 求其所有子序列的个数.注意相同的只算一次:例如 {1,2,1}有子序列{1} {2} {1,2} {2,1}和{1,2, ...
- 1.1(学习笔记)Servlet简介及一个简单的实例
一.Servlet简介 Servlet是使用Java语言编写的服务器端程序,可以生产动态的Web界面. 主要运行在服务器端,Servlet可以方便的处理客户端传来的HTTP请求,并返回一个响应. 二. ...
- 浙南联合训练赛 B-Laptops
One day Dima and Alex had an argument about the price and quality of laptops. Dima thinks that the m ...
- Swift中TableViewCell便利构造器写法
目前为止比较方便的一种方法,如果有更好的写法请通知我,谢谢!
- JNI之常用函数大全
要素 :1. 该函数大全是基于C语言方式的,对于C++方式可以直接转换 ,例如,对于生成一个jstring类型的方法转换分别如下: C编程环境中使用方法为:(*env) ->NewString ...
- JNI之数组
Array Operations -- 数组操作 1.GetArrayLength jsize GetArrayLength(JNIEnv *env, jarray array); Returns t ...