远程服务通讯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 ...
随机推荐
- session喜欢丢值且占内存,Cookis不安全,用什么可以代替呢?
localstorage sessionstorage 在线资料 webdb 这些都是基于HTML5的新特性! 此外还可以使用服务器文件.DB等.
- C#注册表操作类(完整版) 整理完整
/// <summary> /// 注册表基项静态域 /// /// 主要包括: /// 1.Registry.ClassesRoot 对应于HKEY_CLASSES_ROOT主键 /// ...
- 3.C#WebAPI设置路由和参数2
1.上面已经教大家如何修改全局路由了,那么修改完后我们在post请求的要这样使用,其中model模型我就默认你应该已经建好了,没有创建的话请看上一部分 Post方法的参数,如果提交的请求体需要是pho ...
- Dapper.Contrib.Extensions问题
Dapper.Contrib.Extensions问题 Dapper.Extension.1.0.0.1\lib\net45\Dapper.Extension.dll Dapper.Contrib.1 ...
- pageadmin CMS网站建设教程:网页设计的常用参数
由于网络速度问题,我们需要考虑图片大小对传输速度的影响,如果图片太大就会影响浏览速度,访问者很快就会对这个网站失去了兴趣,只有充分了解图片质量与下载速度的关系,并了解不同的文件格式,才能更有效的表达内 ...
- iOS 应用隐藏状态栏
有时候在 Storyboard 和 target 里的设定并不足以使得应用能够完全隐藏状态栏. 这时候需要到 Info.plist,添加一项:View controller-based status ...
- “全栈2019”Java异常第一章:什么是异常?
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java异 ...
- 查看npm全局安装位置
查看npm全局安装位置:npm config get prefix 设置位置:npm config set prefix 填写位置
- Java中常见的jar包及其主要用途
jar包 用途 axis.jar SOAP引擎包 commons-discovery-0.2.jar 用来发现.查找和实现可插入式接口,提供一些一般类实例化.单件的生命周 ...
- Mybatis中的@Param注解
前言 今天在使用Mybatis时遇到一个问题,主要出错原因在于Mybatis的@Param注解,如果我不在参数前面加上@Param注解,数据库操作就会报错,如下: @Param作用 @Param注解的 ...