在下面例子中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. Python小代码

    from bs4 import BeautifulSoup import requests url = 'http://www.tripadvisor.cn/Attractions-g60763-Ac ...

  2. CNN入门笔记

    在之前的学习中,没有认真了解卷积神经网络,由于一些原因需要使用CNN来做图像分类,开始学习了卷积神经网络,参考了一些资料并做了这份记录 为什么要用卷积神经网络 在图像处理中,往往把图像表示为像素的向量 ...

  3. MNIST数据集入门

    简单的训练MNIST数据集 (0-9的数字图片) 详细地址(包括下载地址):http://www.tensorfly.cn/tfdoc/tutorials/mnist_beginners.html # ...

  4. html_table表格

    ## `table`表格 表格的常用标签 - `table`表格- `thead`表格头- `tbody`表格主体- `tfoot`表格尾- `th`元素定义表头单元格- `tr`定义表格行- `td ...

  5. License分类 + 引入开源软件时License的注意事项

    License分类 GPL: linux.openJDK,openJFX,mysql 融合感染,单独子模块不感染(自己的模块与引入模块的通信方式是socket) openJDK(GNU General ...

  6. HAproxy目录分发

    ServerA服务的数据查询接口:/api/v1/datas HAproxy地址10.66.222.333将ServerA添加转发规则添加到HAproxyhaproxy.cfg frontend ht ...

  7. 2013年6月编程语言排行榜,C语言位据第一位

    2013年6月编程语言排行榜,C语言位据第一位 C语言是很多主流开发语言的母体,.NET的底层,Java的底层都是C开发的,虽然很多新语言来势汹汹,但究其根源,都脱离不开C语言

  8. 63. sqlserver查版本号问题

    SELECT @@VERSION as 版本详细情况 SELECT SERVERPROPERTY('edition') as 软件版本 SELECT SERVERPROPERTY('ProductVe ...

  9. as2 fla 关于影片在时间轴的问题

    多帧上面放着没实例名而且里面内容一致的影片,主要一开始读取了,那么跳帧的时候.会自动获取当前帧上的相同内容的影片. 但如果内容不一致的影片,那么在跳帧后,不会获取当前的影片,旧的影片也无法获取.只在当 ...

  10. sqlserver还原数据库

    该方法只针对同等级数据库,不能跨级   比如sqlserver2012还原到sqlserver2008会报错 用数据库日志文件对数据库进行还原一 将日志文件.mdf文件和.ldf文件copy放置在sq ...