Android中AIDL的理解与使用(二)——跨应用绑定Service并通信
跨应用绑定Service并通信:
1、(StartServiceFromAnotherApp)AIDL文件中新增接口:
void setData(String data);
AppService文件中实现接口:
public IBinder onBind(Intent intent) {
return new IAppServiceRomoteBinder.Stub() {
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
}
@Override
public void setData(String data) throws RemoteException {
}
};
}
2、(StartServiceFromAnotherApp)修改AppService内部数据:
private String data = "默认信息";
public void setData(String data) throws RemoteException {
AppService.this.data = data;
}
3、((StartServiceFromAnotherApp)AppService)
onCreat写线程,onDestroy中销毁,每隔一秒输出data,方便测试内部数据的变化:
private boolean running = false;
public void onCreate() {
super.onCreate();
System.out.println("Service started");
new Thread(){
@Override
public void run() {
super.run();
running = true;
while(running){
System.out.println(data);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
public void onDestroy() {
super.onDestroy();
System.out.println("Service destroy");
running = false;
}
4、(AnotherApp)主布局:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="这是另一个应用中的数据"
android:id="@+id/etInput" />
<Button
android:text="同步数据到绑定的服务中"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnSync" />
5、(AnotherApp)进入MainActivity,按钮及输入文本的监听:
private EditText etInput;
etInput = (EditText) findViewById(R.id.etInput);
findViewById(R.id.btnSync).setOnClickListener(this);
6、如何通过Binder方便地执行远程函数?把StartServiceFromAnotherApp中AIDL文件拷贝到AnotherApp中,保持包名一致。
7、(AnotherApp)进行数据最后处理:
private IAppServiceRomoteBinder binder = null;
case R.id.btnUnbindService:
unbindService(this);
binder = null;
break;
case R.id.btnSync:
if(binder != null){
try {
binder.setData(etInput.getText().toString());
} catch (RemoteException e) {
e.printStackTrace();
}
}
public void onServiceConnected(ComponentName name, IBinder service) {
//binder = IAppServiceRomoteBinder(service); 两个类定义所在地址不一样,不能强制类型转换
binder = IAppServiceRomoteBinder.Stub.asInterface(service);
}
Android中AIDL的理解与使用(二)——跨应用绑定Service并通信的更多相关文章
- Android中AIDL的理解与使用(一)——跨应用启动/绑定Service
AIDL(Android Interface Definition Language)--安卓接口定义语言 一.startService/stopService 1.同一个应用程序启动Service: ...
- Android中Context的理解及使用(二)——Application的用途和生命周期
实现数据共享功能: 多个Activity里面,可以使用Application来实现数据的共享,因为对于同一个应用程序来说,Application是唯一的. 1.实现全局共享的数据App.java继承自 ...
- (七)Android中AIDL的应用与理解
一.跨应用启动Service Intent serviceIntent=new Intent();serviceIntent.setComponent(new ComponentName(" ...
- Android中AIDL通信机制分析
一.背景 ·1.AIDL出现的原因 在android系统中,每一个程序都是运行在自己的进程中,进程之间无法进行通讯,为了在Android平台,一个进程通常不能访问另一个进程的内存空间,所以要想对话,需 ...
- Android 中AIDL的使用与理解
AIDL的使用: 最常见的aidl的使用就是Service的跨进程通信了,那么我们就写一个Activity和Service的跨进程通信吧. 首先,我们就在AS里面新建一个aidl文件(ps:现在AS建 ...
- Android笔记——Android中数据的存储方式(二)
我们在实际开发中,有的时候需要储存或者备份比较复杂的数据.这些数据的特点是,内容多.结构大,比如短信备份等.我们知道SharedPreferences和Files(文本文件)储存这种数据会非常的没有效 ...
- Android中一个经典理解误区的剖析
今天,在Q群中有网友(@广州-包晴天)发出了网上的一个相对经典的问题,问题具体见下图. 本来是无意写此文的,但群里多个网友热情不好推却,于是,撰此文予以分析. 从这个问题的陈述中,我们发现,提问者明显 ...
- Android中的一些基础知识(二)
这几天在回顾Android的基础知识,就把一些常见的知识点整理一下,以后忘了也可以翻出来看一看. 简单介绍一下Activity的生命周期 在API文档中对生命周期回调的函数描述的很详细,这里我只是翻译 ...
- Android笔记(四十) Android中的数据存储——SQLite(二) insert
准备工作: 我们模拟一个注册的页面,先看UI 我们需要创建一个数据库:user,数据库包含表user,user表包含字段id.username.password.mobilephone MainAct ...
随机推荐
- Linux下MySQL慢查询分析mysqlsla安装使用
说明: 操作系统:CentOS 5.X 64位 MySQL版本:mysql-5.5.35 MySQL配置文件:/etc/my.cnf MySQL 数据库存放目录:/data/mysql 实现目的:开启 ...
- Mac配置PHP
前言 在MacOS中已经内置了PHP和Apache,所以不需要再额外安装它们,只需要简单几步即可运行PHP. 配置Apache 查看Apache版本: $ sudo apachectl -v 终端关闭 ...
- 萌新笔记——git的问题(error: object file .git/objects/* is empty...)的解决方案及对git版本库文件的了解
由于操作不当,导致git版本库出了大问题,如下所示: error: object file .git/objects/8b/61d0135d3195966b443f6c73fb68466264c68e ...
- PHP严重致命错误处理:php Fatal error: Cannot redeclare class or function
1.错误类型:PHP致命错误 Error type: PHP Fatal error Fatal error: Cannot redeclare (a) (previously declared in ...
- 初识Android Studio
刚开始接触Android Studio,很多不适应的地方,自己慢慢摸索,记录下了一些问题和解决途径. 为了能使用android虚拟机,需要下载镜像,镜像有基于arm架构的也有基于intelx86.x6 ...
- plain framework 商业版 开发总结2 项目管理器
任何事情都有三个阶段,分析.制作.质检的过程.在程序中就分为设计.编码.调试(测试)三个阶段,其中设计最为重要,设计的不好会导致编码和调试重复,甚至最后又回到了设计的过程.为了不会重复返工,所以设计的 ...
- 一枚招聘信息——分期乐招页面重构/UI开发(8k-12k 深圳 经验1-3年 学历不限 全职)
腾讯人创建的分期乐公司,急需页面重构人才若干枚,公司前景好,机会难得,有兴趣的速速 8k-12k 深圳 经验1-3年 学历不限 全职 公司网站: http://www.fenqile.com/ 职位诱 ...
- 安全测试 - XSS如何防御
XSS主要是通过劫持用户COOKIE,执行JS脚本进行攻击 如何发现: 可以使用<script>alert(/yourname/)</script> script最具有代表性也 ...
- [LeetCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 《MySQL 必知必会》读书总结
这是 <MySQL 必知必会> 的读书总结.也是自己整理的常用操作的参考手册. 使用 MySQL 连接到 MySQL shell>mysql -u root -p Enter pas ...