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 ...
随机推荐
- Linux常见英文报错中文翻译(菜鸟必知)
Linux常见英文报错中文翻译(菜鸟必知) 1.command not found 命令没有找到 2.No such file or directory 没有这个文件或目录 3.Permission ...
- [UE4]C++代码操作SplineMesh
转自:http://aigo.iteye.com/blog/2279503 void ARaceSpline::OnConstruction(const FTransform& Transfo ...
- 新手搭建 nginx + php (LNMP)
配置源 纯净的Centos 6.5系统 配置163yum源 (这个比较简单,百度能解决很多问题) 开始 安装 开发软件包:yum -y groupinstall "Developmen ...
- 【求助】win 2008 R2 远程桌面多用户,破解最大连接数2的限制
[求助]win 2008 R2 远程桌面多用户,破解最大连接数2的限制. 1. 本地组策略设置的是“允许的RD最大连接数 5”. 2. 远程桌面仍然只能有两个连接在线. 3. 后来发现是下面这个设置限 ...
- linux操作系统1 Linux入门
知识内容: 1.操作系统简介 2.Linux操作系统简介 3.Linux安装 4.Linux各大发行版本介绍 一.操作系统介绍 1.为什么需要操作系统 现代的计算机系统由处理器,主存,硬盘,键盘,鼠标 ...
- windows10系统右键添加cmd命令
https://blog.csdn.net/Mr_BEelzebub/article/details/78776104 首先,在桌面新建一个文本文档. Windows Registry Editor ...
- leetcode210
public class Solution { //test case [1,0] public int[] findOrder(int numCourses, int[][] prerequisit ...
- SQL SERVER2014 安装 Error code 0x858C001B.
原因是语言版本不一致,SQL SERVER是中文简体版,操作系统是英文版,在操作系统.控制面板,区域语言设置为中文就Ok啦. TITLE: SQL Server Setup failure.----- ...
- parseInt 和 parseFloat 实现字符串转换为数字
age = '18' a = parseInt(age) b = parseFloat(age)
- vue深入了解组件——Prop
一.Prop的大小写(camelCase vs kebab-case) HTML中的特性名是大小写不敏感的,所以浏览器会把所有大写字符解释为小写字符.这意味着当你使用DOM中的模板时,cameCase ...