在下面例子中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--------应用之间的通信接口的更多相关文章

  1. AIDL示例

    背景 最近在考虑项目重构的时候,考虑将项目拆分成两个APK,一个用于数据服务,一个用于UI展示. 数据服务APK向自己编写APK提供数据,同时也可以向第三方提供数据.考虑使用这样的方式代替向第三方提供 ...

  2. Android AIDL Service

    AIDL Service //跨进程调用Service    一个APK想调用另一个APK中的Service 如何实现AIDL //定义两个进程之间的通信接口 Demo1 传递简单数据 AidlSer ...

  3. 机器人操作系统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 ...

  4. 可编程逻辑(FPGA)与硬核处理器(HPS)之间互联的结构

    本周我想进一步探究可编程逻辑(FPGA)与硬核处理器(HPS)之间互联的结构.我发现了三种主要方式,它们是如何映射并处理通信的,哪些组件需要管控时序并且有访问权限. AXI Bridge 为了能够实现 ...

  5. 2016总结Android面试题

    1.简单的设计模式:单例模式:在系统中一个类只有一个实例. 分为懒汉模式和饿汉模式.饿汉模式的代码如下:public class Singleten{private static singleten ...

  6. android学习笔记57——Service_2

    Service生命周期 参考:http://codingnow.cn/android/515.html 应用程序启动服务的方式不同,其生命周期也有所不同. startService生命周期如下左图: ...

  7. 19、android面试题整理(自己给自己充充电吧)

    (转载,出处丢失,请原作者原谅,如有意见,私信我我会尽快删除本文) JAVA 1.GC是什么? 为什么要有GC?GC是垃圾收集的意思(Gabage Collection),内存处理是编程人员容易出现问 ...

  8. Android Framework 其中A记录

    一个简短的引论 以往的研究太偏应用层的功能,实现了,原则上不进入非常理解,现在,研究人员framework该框架层. 创纪录的 1.下载源代码,文件夹例如以下: 2.Android系统的层次例如以下: ...

  9. Android 服务_笔记

    Service服务 服务(Service)是Android中的四大组件之一,适用于开发无界面.长时间运行的应用功能,服务是在后台运行,服务的创建与Activity类似,只需要继承Service和在An ...

随机推荐

  1. 使用Golang进行性能分析(Profiling)

    转自:http://www.cppblog.com/sunicdavy/archive/2015/04/11/210308.html 本文介绍游戏服务器的性能分析, web服务器性能分析不在本文分析范 ...

  2. osx 安装redis

    brew install redis 想关文章 http://www.tuicool.com/articles/nM73Enr http://www.iteye.com/topic/1124400

  3. Doris FE负载均衡配置

    0 背景概述 Doris完全兼容了mysql协议,并且Doris FE本身通过多follower选举机制选举出master,可以保证fe本身的高可用性,也可以通过加入observer fe节点来提高f ...

  4. solr5.3的spellcheck功能

    1.增加schema.xml中的检查字段. <field name="title" type="text_cn" indexed="true&q ...

  5. relocation error: /usr/lib64/libc.so.6: symbol _dl_starting_up, version GLIBC_PRIVATE not defined in file ld-linux-x86-64.so.2 with link time reference 问题解决

    在建立一个错误的软连接到ld-linux-x86-64.so.2时,悲剧就这么发生了.此时大部分命令都不能使用,SSH当然也不能登录了.这个时候一定不要退出终端. 有人说那就把软连接复原吧,可是ln也 ...

  6. C# window Service实现调用有UI的应用程序(关于win xp以后的window系统)

    我开发的系统中有一接口程序(这里就称Task,是一个C#的Console Application)经常无故的死掉,导致第二天的数据不能正常解析,所以,我写了一个window service去监视Tas ...

  7. python带参数装饰器使用

    # -*- coding: utf-8 -* """TensorFlow指定使用GPU工具类 author: Jill usage: 方法上加@tf_with_devic ...

  8. js 获取上传文件的字节数及内容

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. 下载Chrome独立版(alternate/offline Installer)的地方

    因为Great fire wall,Chrome无法更新,下载独立版(alternate/offline Installer)的地方 很多朋友可以用proxy firefox访问google,却无法让 ...

  10. ByteCache

    private static class ByteCache { private ByteCache(){} //256个元素,用于缓存-128到127 static final Byte cache ...