Android 中 Service AIDL使用
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return new IMyAidlInterface.Stub() {
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException { }
};
}
3.启动项目一service1将.aidl文件配置到项目中;
intentt = new Intent();
intentt.setComponent(new ComponentName("com.example.com.myapplication", "com.example.com.myapplication.MyService"));
启动服务
bindService(intentt, this, Context.BIND_AUTO_CREATE);
销毁服务
unbindService(this);
5.返回一个iMyAidlInterface接口
/**
*
* 连接
* @param name
* @param service
*/
@Override
public void onServiceConnected(ComponentName name, IBinder service) { iMyAidlInterface=IMyAidlInterface.Stub.asInterface(service); }
6.传递数据
if(iMyAidlInterface!=null) {
try {
iMyAidlInterface.setDate(et.getText().toString());
} catch (RemoteException e) {
e.printStackTrace();
}
}
7.接受数据并且应用
return new IMyAidlInterface.Stub() {
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException { } @Override
public void setDate(String data) throws RemoteException { MyService.this.data=data;
}
};
服务返回值onBind
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return new IMyAidlInterface.Stub() {
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException { }
};
}
启动服务
bindService(intentt, this, Context.BIND_AUTO_CREATE);
销毁服务
unbindService(this);
/**
* 连接成功
* @param name
* @param service
*/
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// 打印输出
Log.e("App2 BInd service", "StartService"); } /**
* 连接失败
* @param name
* @param
*/
@Override
public void onServiceDisconnected(ComponentName name) { }
if(iMyAidlInterface!=null) {
try {
iMyAidlInterface.setDate(et.getText().toString());
} catch (RemoteException e) {
e.printStackTrace();
}
}
返回时接受的接口
@Override
public void onServiceConnected(ComponentName name, IBinder service) { iMyAidlInterface=IMyAidlInterface.Stub.asInterface(service); }
在p里面的Service中实现接口并且传值
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return new IMyAidlInterface.Stub() {
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException { } @Override
public void setDate(String data) throws RemoteException { MyService.this.data=data;
}
};
}
在Myservic中使用:
@Override
public void onCreate() {
super.onCreate(); new Thread(new Runnable() {
@Override
public void run() { run=true;
while (run){ try {
Thread.sleep(1000);
Log.e("service", data);
} catch (InterruptedException e) {
e.printStackTrace();
} } }
}).start(); Log.e("service", "startServier");
}
Android 中 Service AIDL使用的更多相关文章
- Android 中的AIDL,Parcelable和远程服务
Android 中的AIDL,Parcelable和远程服务 早期在学习期间便接触到AIDL,当时对此的运用也是一撇而过.只到近日在项目中接触到AIDL,才开始仔细深入.AIDL的作用 ...
- Android中Service的使用详解和注意点(LocalService)
Android中Service的使用详解和注意点(LocalService) 原文地址 开始,先稍稍讲一点android中Service的概念和用途吧~ Service分为本地服务(LocalServ ...
- Android中Service(服务)详解
http://blog.csdn.net/ryantang03/article/details/7770939 Android中Service(服务)详解 标签: serviceandroidappl ...
- Android中Service的一个Demo例子
Android中Service的一个Demo例子 Service组件是Android系统重要的一部分,网上看了代码,很简单,但要想熟练使用还是需要Coding. 本文,主要贴代码,不对Servic ...
- 【转载】Android Studio Service AIDL 详解
公司产品之前IM这块存在很多问题,消息到达率低,加上协议上有些问题,丢消息频繁,所以需要重构IM,AIDL不能解决以上问题.好吧!那AIDL可以解决什么问题?什么是AIDL? 什么是AIDL? AID ...
- Android中Service 使用详解(LocalService + RemoteService)
Service 简介: Service分为本地服务(LocalService)和远程服务(RemoteService): 1.本地服务依附在主进程上而不是独立的进程,这样在一定程度上节约了资源,另外L ...
- android中的AIDL进程间通信
关于IPC应该不用多介绍了,Android系统中的进程之间不能共享内存,那么如果两个不同的应用程序之间需要通讯怎么办呢?比如公司的一个项目要更新,产品的需求是依附于当前项目开发一个插件,但是呢这个插件 ...
- Android中Service深入学习
概述 1.当用户在与当前应用程序不同的应用程序时,Service可以继续在后台运行. 2.Service可以让其他组件绑定,以便和它交互并进行进程间通信. 3.Service默认运行在创建它的应用程序 ...
- android中的AIDL学习笔记
一.定义 AIDL是用来解决进程间通信的(一般有四种方式:Activity.Service.ContentProvider.Broadcast Receiver),两个进程间无法直接通信,所以要用AI ...
随机推荐
- java数组获取最值
import java.util.Random; /** * 获取最大值最小值 * @author shaobn * */ public class Test2 { public static voi ...
- GitHub和SourceTree入门教程——(转载),希望能帮到有需要的人
-->本教程适用于主流的开源网站github和bitbucket,个人认为sourceTree还是比较好用的git客户端,支持windows和mac os. -->soureceTree的 ...
- sql 修改字段长度以及其他属性
修改字段长度 语法 :alter table <表名> alter column <字段名> 新类型名(长度) alter table LoaneeExpand alt ...
- oracle数据库的归档模式
1:开发环境和测试环境中,数据库的日志模式和自动归档模式一般都是不设置的,这样有利于系统应用的调整,也免的生成大量的归档日志文件将磁盘空间大量的消耗. 2:生产环境时,将其设置为日志模式并自动归档就相 ...
- 夺命雷公狗ThinkPHP项目之----企业网站16之文章批量删除的完成
我们在做一个网站时候经常会遇到批量删除这个选项,其实也很简单,我们之需要用用jq实现出来效果然后通过表单post过去后端即可实现: 我们做这个功能前必须要先引入jq,我的jq版本是1.8.3,方法如下 ...
- 关于nandflash与norflash
读取速度:nor > nand 写入速度:nand > nor 擦除速度:nand 4ms,nor 5s nand的擦除单元更小,相应的擦除电路更少. nand的实际应用方式比nor复杂, ...
- zw版_Halcon图像库delphi接口文件
zw版_Halcon图像库delphi接口文件 Halcon图像库delphi接口文件,根据安装时用户设置的文件目录不同,会有所差异,笔者一般安装在delphi的import目录下. 参见:& ...
- javascript中字符串格式json如何转化成json对象
什么是JSON JSON(JavaScript Object Notation)是一种优美的JavaScript对象创建方法.JSON也是一种轻量级数据交换格式.JSON非常易于人阅读与编写,同时利于 ...
- NOIP200505谁拿了最多的奖学金
NOIP200505谁拿了最多的奖学金 Description 某校的惯例是在每学期的期末考试之后发放奖学金.发放的奖学金共有五种,获取的条件各自不同: 1) 院士奖学金,每人8000元,期 ...
- SlidingMenu实现app侧滑功能
很多APP都有侧滑菜单的功能,部分APP左右都是侧滑菜单~SlidingMenu 这个开源项目可以很好帮助我们实现侧滑功能,如果对SlidingMenu 还不是很了解的童鞋,可以参考下本篇博客. 本片 ...