在下面例子中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. javascript cookie操作.

    给cookie追加name: document.cookie="name="+escape("我叫钢蛋");//这里可以不写secape,这样写会更加的安全., ...

  2. JS获取url传参

    function GetRequest() { var url = location.search; //获取url中"?"符后的字串 var theRequest = new O ...

  3. storm的流分组

    用的是ShuffleGrouping分组方式,并行度设置为3 这是跑下来的结果 参考代码StormTopologyShufferGrouping.java package yehua.storm; i ...

  4. 关于QT中“崩溃”问题

    经常会遇到一个问题,程序运行崩溃! 1.release.debug直接运行崩溃. 2.程序可以运行但是点击界面崩溃. 3.debug模式崩溃,release正常. 4.软件里面的release和deb ...

  5. Pillow 读取图片截断错误

    做图像处理的时候,Pillow是经常使用到的模块. 最近在读取图片的时候出现了一个错误. OSError: image file is truncated (461 bytes not process ...

  6. oracle库和表空间

    完整的Oracle数据库通常由两部分组成:Oracle数据库和数据库实例. 1) 数据库是一系列物理文件的集合(数据文件,控制文件,联机日志,参数文件等): 2) Oracle数据库实例则是一组Ora ...

  7. Enum学习中的compareTo方法分析

    今天看工厂模式的时候里面用了枚举定义各种可能的实例类型,就看了一下枚举,发现里面有一个compareTo(E o)方法 通过Object的getClass()方法比较两个两个比校对象类型是否一致,如果 ...

  8. Haskell语言学习笔记(28)Data.Map

    Map Prelude> import Data.Map as Map Prelude Map> :set -XOverloadedLists Prelude Map> Overlo ...

  9. python中时间差中seconds和total_seconds

    在python中经常会用到计算两个时间差,两个日期类型进行相减可以获取到时间差 经常会使用seconds来获取,其实seconds获取的是时间差的秒数,遗漏了天 seconds是获取时间部分的差值,而 ...

  10. js中定时器之一

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...