远程服务通讯Service(Remote--AIDL)
服务端代码: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)的更多相关文章
- Android service binder aidl 关系
/********************************************************************************** * Android servic ...
- Android学习笔记_23_服务Service之AIDL和远程服务实现进程通信以及进程间传递自定义类型参数
一.了解AIDL语言: 在Android中, 每个应用程序都有自己的进程,当需要在不同的进程之间传递对象时,该如何实现呢? 显然, Java中是不支持跨进程内存共享的.因此要传递对象, 需要把对象解析 ...
- 一个简单的demo学习Android远程Service(AIDL的使用)
这是milo很早之前写在论坛上的一个帖子,现在整理出来,milo也复习一下一般来说Android 的四大组件都是运行在同一个进程中的,但远程Service运行在不同的进程里.这进程间的通信是使用了An ...
- 【起航计划 037】2015 起航计划 Android APIDemo的魔鬼步伐 36 App->Service->Remote Service Binding AIDL实现不同进程间调用服务接口 kill 进程
本例和下个例子Remote Service Controller 涉及到的文件有RemoteService.java ,IRemoteService.aidl, IRemoteServiceCallb ...
- android 远程Service以及AIDL的跨进程通信
在Android中,Service是运行在主线程中的,如果在Service中处理一些耗时的操作,就会导致程序出现ANR. 但如果将本地的Service转换成一个远程的Service,就不会出现这样的问 ...
- 【5】Android Service 与 AIDL
前言:本系列仅介绍基本大体的使用步骤,而不对每个步骤进行细致的讲解.读者可作为已经对相关内容有所了解后的快速查阅. 一.单应用内Service的使用 Service组件与Activity以IBinde ...
- Android四大组件--服务(Service)
1. startService和bindService的区别 1. startService: 生命周期: onCreate---onStartCommand---onDestory 与服务的通讯: ...
- Android(java)学习笔记232:Android进程间通讯(IPC)之AIDL
一.IPC inter process communication 进程间通讯 二.AIDL android interface defination language 安卓接口定义语言 满 ...
- Android Service和Binder、AIDL
1.首先理解service的作用和生命周期 由于activity如果切换,那么他就不再运行,那么我们想在玩游戏的时候听播放器中的音乐,activity就应运而生了,这是最常见的一种场景,同时servi ...
随机推荐
- 学习css(一)
font:12px/22px "\5B8B\4F53",Arial, Helvetica, sans-serif font:12px/22px 指 字体大小/字体行高 " ...
- 微信小程序web-view之动态加载html页面
官方推出的web-view方便了很多开发人员. 我们在做的时候,经常会想到写一个小程序的page然后通过动态加载web-view的形式来完成其他功能页面的开发. 之前研究web-view的时候发现网上 ...
- indows 2008 r2/做了SPS2007---2013后,发现添加原来域中的域组添加不上
根据上次的网络包的分析, 我们在AD中找到了wtc-beijing-it的组, 不过在SharePoint日志中我们没有发现搜索成功的记录. - SearchResultEntry: CN=WTC-B ...
- RabbitMq初探——消息持久化
消息持久化 前言 通过上一节,我们知道,有消息确认机制,保证了当消费者进程挂掉后,消息的不丢失. 但是如果rabbitmq挂掉呢?它的队列和消息都会丢失的.为了保证消息在rabbitmq挂掉重启后不丢 ...
- JAVA判断质数
好久没写了,今天做题有点忘了,不会写了.重新做了一份,整理出来. import java.util.Scanner; public class 判断质数 { public static boolean ...
- 记一次MBR锁机病毒分析
有一天,在机缘巧合之下我获得了一个锁机软件(是多巧合阿喂!),然后兴高采烈的把它拖入了虚拟机里蹂躏(>_<!). 很巧,软件有虚拟机检测... Emmmm好吧,随便过一下... 我用的虚拟 ...
- [CSS3] 二级下拉导航
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="utf-8& ...
- iphone 自学常用网址
https://www.gitbook.com/book/numbbbbb/-the-swift-programming-language-/details https://github.com/ip ...
- mybatis常用默认配置
设置参数 描述 有效值 默认值 cacheEnable 该配置影响所有映射器中配置的缓存全局开关 true.false true lazyLoadingEnable 延迟加载的全局开关.当它开启时,所 ...
- 部署到docker容器后图片验证码显示不出来
Dockerfile如下: FROM openjdk:8-jre-alpineARG JAR_FILECOPY ${JAR_FILE} app.jarENTRYPOINT ["java&qu ...