服务端代码:https://github.com/maogefff/AndroidTest/tree/develop-ServiceLocal2

客户端代码:https://github.com/maogefff/AndroidTest/tree/develop-ServiceRemote2

1. 服务端编写

AndroidManifest.xml:

        <service
android:name=".AIDLService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.TestRemoteService"></action>
<category android:name="android.intent.category.DEFAULT"></category>
</intent-filter>
</service>

IRemoteServiceTest.aidl:

package com.example.tony.servicelocal;

interface IRemoteServiceTest {

    int TestInt(int i);
double TestDouble(double i);
String TestString(String i); }

AIDLService.java

package com.example.tony.servicelocal;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log; public class AIDLService extends Service {
String TAG = "AIDLService"; class RemoteServiceTest extends IRemoteServiceTest.Stub { @Override
public int TestInt(int i) throws RemoteException {
return lTestInt(i);
} @Override
public double TestDouble(double i) throws RemoteException {
return lTestDouble(i);
} @Override
public String TestString(String i) throws RemoteException {
return lTestString(i);
}
}
@Override
public IBinder onBind(Intent intent) {
return new RemoteServiceTest();
}
private int lTestInt(int i){
return i+;
}
private double lTestDouble(double i){
return i+;
}
private String lTestString(String i){
return i+" fuck";
}
}

2. 客户端编写

把服务端的AIDL放入包名相同的AIDL路径中:

MainActivity.java

package com.example.tony.serviceclient;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast; import com.example.tony.servicelocal.IRemoteServiceTest; public class MainActivity extends AppCompatActivity implements View.OnClickListener{
String TAG = "MainActivity";
Button start;
Button testInt;
Button testFloat;
Button testString; Intent it;
IRemoteServiceTest remoteServiceTest; ServiceConnection sc = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
       //这句很重要!!!
remoteServiceTest = IRemoteServiceTest.Stub.asInterface(iBinder);
} @Override
public void onServiceDisconnected(ComponentName componentName) {
Log.d(TAG, "onServiceDisconnected");
remoteServiceTest = null;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); start = (Button)findViewById(R.id.startService);
testInt = (Button)findViewById(R.id.testInt);
testFloat = (Button)findViewById(R.id.testfloat);
testString = (Button)findViewById(R.id.testString); start.setOnClickListener(this);
testInt.setOnClickListener(this);
testFloat.setOnClickListener(this);
testString.setOnClickListener(this);
} @Override
public void onClick(View view) {
switch (view.getId()){
case R.id.startService:
it = new Intent();
//远程服务的intent-filter中的动作
it.setAction("android.intent.action.TestRemoteService");
//IRemoteServiceTest.aidl的包名
it.setPackage("com.example.tony.servicelocal");
if(bindService(it, sc, Service.BIND_AUTO_CREATE)==true)
Toast.makeText(this, "启动服务成功", Toast.LENGTH_SHORT).show();
else
Toast.makeText(this, "启动服务失败", Toast.LENGTH_SHORT).show();
break;
case R.id.testInt:
try {
int i = remoteServiceTest.TestInt();
Toast.makeText(this, "测试整型:i="+i, Toast.LENGTH_SHORT).show();
} catch (RemoteException e) {
e.printStackTrace();
}
break;
case R.id.testfloat:
try {
double i = remoteServiceTest.TestDouble(2.34);
Toast.makeText(this, "测试浮点:i="+i, Toast.LENGTH_SHORT).show();
} catch (RemoteException e) {
e.printStackTrace();
}
break;
case R.id.testString:
try {
String i = remoteServiceTest.TestString("hello world");
Toast.makeText(this, "测试字符串:i="+i, Toast.LENGTH_SHORT).show();
} catch (RemoteException e) {
e.printStackTrace();
}
break;
}
} }

远程服务通讯Service(Remote--AIDL)的更多相关文章

  1. Android service binder aidl 关系

    /********************************************************************************** * Android servic ...

  2. Android学习笔记_23_服务Service之AIDL和远程服务实现进程通信以及进程间传递自定义类型参数

    一.了解AIDL语言: 在Android中, 每个应用程序都有自己的进程,当需要在不同的进程之间传递对象时,该如何实现呢? 显然, Java中是不支持跨进程内存共享的.因此要传递对象, 需要把对象解析 ...

  3. 一个简单的demo学习Android远程Service(AIDL的使用)

    这是milo很早之前写在论坛上的一个帖子,现在整理出来,milo也复习一下一般来说Android 的四大组件都是运行在同一个进程中的,但远程Service运行在不同的进程里.这进程间的通信是使用了An ...

  4. 【起航计划 037】2015 起航计划 Android APIDemo的魔鬼步伐 36 App->Service->Remote Service Binding AIDL实现不同进程间调用服务接口 kill 进程

    本例和下个例子Remote Service Controller 涉及到的文件有RemoteService.java ,IRemoteService.aidl, IRemoteServiceCallb ...

  5. android 远程Service以及AIDL的跨进程通信

    在Android中,Service是运行在主线程中的,如果在Service中处理一些耗时的操作,就会导致程序出现ANR. 但如果将本地的Service转换成一个远程的Service,就不会出现这样的问 ...

  6. 【5】Android Service 与 AIDL

    前言:本系列仅介绍基本大体的使用步骤,而不对每个步骤进行细致的讲解.读者可作为已经对相关内容有所了解后的快速查阅. 一.单应用内Service的使用 Service组件与Activity以IBinde ...

  7. Android四大组件--服务(Service)

    1. startService和bindService的区别 1. startService: 生命周期: onCreate---onStartCommand---onDestory 与服务的通讯: ...

  8. Android(java)学习笔记232:Android进程间通讯(IPC)之AIDL

    一.IPC inter process communication  进程间通讯 二.AIDL android  interface  defination  language  安卓接口定义语言 满 ...

  9. Android Service和Binder、AIDL

    1.首先理解service的作用和生命周期 由于activity如果切换,那么他就不再运行,那么我们想在玩游戏的时候听播放器中的音乐,activity就应运而生了,这是最常见的一种场景,同时servi ...

随机推荐

  1. 经典的兔子生兔子问题(C#递归解法)

    古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 思路:先求出每个月新增的兔子,再用循环求和即可算出这个月 ...

  2. c# in out ref关键字

    class in_out_ref { #region in 关键字 delegate void DContravariant<in A>(A argumen); static void o ...

  3. AngularJS 常用语法

    面板收缩(class=collapsed)与扩展(class=expand) <div ng-init="expand=false" data-ng-class=" ...

  4. 《快学Scala》第八章 继承

  5. Make ISO安装ArchLinux加Cinnamon

    Arch安装一直对大家对普通用户来說一直很难.国外大神为Arch安装进行了优化提供了更方便的安装方式 官网:http://www.evolutionlinux.com/ 以下爲个人理解,供大家参考. ...

  6. 高性能缓存服务器Varnish

    一.Varnish概述 Varnish是一款高性能的.开源的反向代理服务器和缓存服务器,计算机系统的除了有内存外,还有CPU的L1.L2,甚至L3级别的缓存,Varnish的设计架构就是利用操作系统的 ...

  7. re 模块 常规方法使用

    前情提要: re模块主要用于正则,用的好了秒杀一切匹配的规则,这里主要是介绍基本用法 一:元字符 1:\w 匹配字符,包含中文,数字或下划线 l ='早乙女露依 123 是我的 321 心目中的 22 ...

  8. Springmvc之表单验证

    1.需要的相关jar 这里采用的是hibernate-validator-5.2.4.Final 和validation-api-1.1.0.Final 两个jar包.Hibernate Valida ...

  9. video.js 应用于网站需要视频的

    http://www.cnblogs.com/lechenging/p/3858181.html

  10. springmvc 运行原理 Spring ioc的实现原理 Mybatis工作流程 spring AOP实现原理

    SpringMVC的工作原理图: SpringMVC流程 . 用户发送请求至前端控制器DispatcherServlet. . DispatcherServlet收到请求调用HandlerMappin ...