AIDL--------应用之间的通信接口
在下面例子中04Service中添加aidl包包里定义好接口 接口文件名后缀为.aidl
package com.example.aidl;
interface IRemoteService
{
void print(String msg);
String getName();
}
在04 client中也有这样一个包 这个包就成为一个接口 Service里实现接口 client中可以调用注意
Service中定义好service的action client中绑定服务时用这个action
<service android:name=".RemoteService">
<intent-filter >
<action android:name="com.example.service04aidl.RemoteService"/>
</intent-filter>
</service>
绑定时用到
bindService(new Intent("com.example.service04aidl.RemoteService"),conn,BIND_AUTO_CREATE);
客户端每次绑定都会创建一个与客户端activity绑定的Service activity销毁服务也会销毁
package com.example.service04_client; 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;
import android.view.View;
import android.widget.Toast; import com.example.aidl.IRemoteService; public class MainActivity extends Activity { IRemoteService remoteService;
ServiceConnection conn=new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) { } @Override
public void onServiceConnected(ComponentName name, IBinder service) {
remoteService=IRemoteService.Stub.asInterface(service);
}
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); bindService(new Intent("com.example.service04aidl.RemoteService"),conn,BIND_AUTO_CREATE);
}
public void print(View v) throws RemoteException
{
if(remoteService!=null)
remoteService.print("hello,service");
}
public void getRemoteName(View v) throws RemoteException
{
if(remoteService!=null)
Toast.makeText(getApplicationContext(), remoteService.getName(), 1).show();
} }
04Service—mainActivity
package com.example.service04aidl; import com.example.aidl.IRemoteService; import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log; public class RemoteService extends Service { private IRemoteService.Stub stub=new IRemoteService.Stub() {
@Override
public void print(String msg) throws RemoteException {
Log.i("debug", "RemoteService---"+msg);
}
@Override
public String getName() throws RemoteException {
return "RemoteService";
}
};
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return stub;
} }
04Service--RemoteService
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.service04aidl"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.service04aidl.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".RemoteService">
<intent-filter >
<action android:name="com.example.service04aidl.RemoteService"/>
</intent-filter>
</service>
</application> </manifest>
04----xml
package com.example.service04_client; 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;
import android.view.View;
import android.widget.Toast; import com.example.aidl.IRemoteService; public class MainActivity extends Activity { IRemoteService remoteService;
ServiceConnection conn=new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) { } @Override
public void onServiceConnected(ComponentName name, IBinder service) {
remoteService=IRemoteService.Stub.asInterface(service);
}
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); bindService(new Intent("com.example.service04aidl.RemoteService"),conn,BIND_AUTO_CREATE);
}
public void print(View v) throws RemoteException
{
if(remoteService!=null)
remoteService.print("hello,service");
}
public void getRemoteName(View v) throws RemoteException
{
if(remoteService!=null)
Toast.makeText(getApplicationContext(), remoteService.getName(), 1).show();
} }
client
AIDL--------应用之间的通信接口的更多相关文章
- AIDL示例
背景 最近在考虑项目重构的时候,考虑将项目拆分成两个APK,一个用于数据服务,一个用于UI展示. 数据服务APK向自己编写APK提供数据,同时也可以向第三方提供数据.考虑使用这样的方式代替向第三方提供 ...
- Android AIDL Service
AIDL Service //跨进程调用Service 一个APK想调用另一个APK中的Service 如何实现AIDL //定义两个进程之间的通信接口 Demo1 传递简单数据 AidlSer ...
- 机器人操作系统ROS(indigo)与三维仿真软件V-Rep(3.2.1)通信接口使用笔记
关键字:ROS(indigo),V-Rep(3.2.1), vrep_ros_bridge(lagadic). vrep_ros_bridge提供了V-Rep和ROS之间的通信接口,可以实现使用ROS ...
- 可编程逻辑(FPGA)与硬核处理器(HPS)之间互联的结构
本周我想进一步探究可编程逻辑(FPGA)与硬核处理器(HPS)之间互联的结构.我发现了三种主要方式,它们是如何映射并处理通信的,哪些组件需要管控时序并且有访问权限. AXI Bridge 为了能够实现 ...
- 2016总结Android面试题
1.简单的设计模式:单例模式:在系统中一个类只有一个实例. 分为懒汉模式和饿汉模式.饿汉模式的代码如下:public class Singleten{private static singleten ...
- android学习笔记57——Service_2
Service生命周期 参考:http://codingnow.cn/android/515.html 应用程序启动服务的方式不同,其生命周期也有所不同. startService生命周期如下左图: ...
- 19、android面试题整理(自己给自己充充电吧)
(转载,出处丢失,请原作者原谅,如有意见,私信我我会尽快删除本文) JAVA 1.GC是什么? 为什么要有GC?GC是垃圾收集的意思(Gabage Collection),内存处理是编程人员容易出现问 ...
- Android Framework 其中A记录
一个简短的引论 以往的研究太偏应用层的功能,实现了,原则上不进入非常理解,现在,研究人员framework该框架层. 创纪录的 1.下载源代码,文件夹例如以下: 2.Android系统的层次例如以下: ...
- Android 服务_笔记
Service服务 服务(Service)是Android中的四大组件之一,适用于开发无界面.长时间运行的应用功能,服务是在后台运行,服务的创建与Activity类似,只需要继承Service和在An ...
随机推荐
- 深入理解yield(三):yield与基于Tornado的异步回调
转自:http://beginman.cn/python/2015/04/06/yield-via-Tornado/ 作者:BeginMan 版权声明:本文版权归作者所有,欢迎转载,但未经作者同意必须 ...
- Android如何使用Https
什么是Https? HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全 ...
- SPM——Using Maven+Junit to test Hello Wudi
Last week, ours teacher taught us 'Software Delivery and Build Management'. And in this class, our t ...
- tcp 大文件上传 ,切换目录 及登陆文件加盐处理
实现大文件的传输 服务器 import socketimport jsonimport structsk = socket.socket()sk.bind(("127.0.0.1" ...
- openssl - cookbook
1.openssl 2.Testing 3.Best Practices last 1.openssl 1.1.Key and Cerificate Management Run a web serv ...
- Postman的基础使用
postman的基础功能,官方文档介绍的是相当啰嗦,所以笔者这里先简单介绍一下主界面,入门功能就都提到了.稍后我们再一一介绍基础功能的使用方法. Collections:在Postman中,Colle ...
- Javascript,获取元素,write方法
一:Javascript:弱类型脚本语言,是一种动态类型.实现部分动画效果和用户交互等 -- html是骨架(页面结构) css样式 js是行为 -- 弱类型体现: JS代码可以写在body,he ...
- Tensorflow线程和队列
读取数据 小数量数据读取 这仅用于可以完全加载到存储器中的小的数据集有两种方法: 存储在常数中. 存储在变量中,初始化后,永远不要改变它的值. 使用常数更简单一些,但是会使用更多的内存,因为常数会内联 ...
- bean-json-bean-json 工具
package com.taotao.utils; import java.util.List; import com.fasterxml.jackson.core.JsonProcessingExc ...
- framework4.0 IIS配置支持ashx
framework4.0 https://www.microsoft.com/zh-cn/download/details.aspx?id=17718 IIS添加对ashx文件的支持 http://w ...