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可 ...
随机推荐
- 转怎么让VI支持中文显示
https://blog.csdn.net/kayneo/article/details/72957475
- 跨机房openvpn互联
实现目标:北京的client能访问上海的client (1)vpn server配置 1)基本配置 关闭selinux sed -ri '/^SELINUX=/cSELINUX=disabled' / ...
- Spring的自动装配
在Spring中对自定义的引用类型注入时可以实现自动赋值.但是必须依赖set方法: 自动装配功能有两种: <!-- autowire:"byType" --根据class匹 ...
- 使用selenium模拟知网登录
之前都是用phantomjs和selenium模拟浏览器动作的,后来phantomjs不再更新,就转用chrome了 本次模拟登录的网站是中国知网http://login.cnki.net/login ...
- 谜题27:变幻莫测的i值
与谜题26中的程序一样,下面的程序也包含了一个记录在终止前有多少次迭代的循环.与那个程序不同的是,这个程序使用的是左移操作符(<<).你的任务照旧是要指出这个程序将打印什么.当你阅读这个程 ...
- 记录(Record)
记录有可以被称为行(Row),可以通俗的认为它是数据表中的一行数据.以员工表为例,一个公司的员工表中的数据是这样的: 这里每一行数据就代表一个员工的资料,这样的一行数据就叫做一条记录.表是由行和列组成 ...
- 对于scanf和cin的输入输出速度的验证
本文为https://www.byvoid.com/zhs/blog/fast-readfile的验证性文章 --------------------------------------------- ...
- AtCoder - 2567 RGB Sequence
Problem Statement There are N squares arranged in a row. The squares are numbered 1, 2, …, N, from l ...
- 【2-SAT】Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) D. Innokenty and a Football League
先反复地扫(不超过n次),把所有可以确定唯一取法的给确定下来. 然后对于剩下的不能确定的,跑2-SAT.输出可行解时,对于a和¬a,如果a所在的强连通分量序号在¬a之前,则取a,否则不取a.如果a和¬ ...
- [Android]Android 布局中如何让图片和文字居中显示?
图片文字居中显示 **①组件TextView的属性 drawableTop ``` <LinearLayout android:layout_width="match_parent&q ...