服务端代码: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. angular响应式表单 - 状态字段

    用于表单验证的过程: touched,untoched pristine,dirty pending

  2. WPF自定义进度条

    <!--进度条 4812--> <LinearGradientBrush x:Key="ProgressBarIndicatorAnimatedFill" Sta ...

  3. dokcer 的export 、improt和save 、load

    export .improt 是对容器操作也就是类似于虚拟机的快照 save .load 是针对于镜像操作的..

  4. 【OCP-12c】CUUG 071题库考试原题及答案解析(13)

    13.(6-7) choose twoWhich two statements are true regarding operators used with subqueries? (Choose t ...

  5. kali linux之Audacity

    常用音频隐写工具 安装: sudo apt install audacity 初次打开的界面 看波的宽度分辨长短音 比较细的就是短音,代表".",比较粗的就是长音,代表" ...

  6. [原]时间格式化hh:mm:ss和HH:mm:ss区别

    hh:mm:ss   按照12小时制的格式进行字符串格式化 如果时间处于00:00:00——12:59:59,则返回的字符串正常 如果时间处于13:00:00——23:59:59,则返回的字符串是实际 ...

  7. PHP错误——Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes)

    解释是可用内存已耗尽,这关系到PHP的memory_limit的设置问题. 这里有两种方法解决 1.修改php.ini memory_limit = 128 打开终端输入下列bash命令 cd /pr ...

  8. leetcode-867-Transpose Matrix(矩阵由按行存储变成按列存储)

    题目描述: Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped o ...

  9. Mysql 四种事务隔离介绍以及锁机制

    还有很多不太懂,这里收集几份大佬文章“飞机票”,待我整理好了,再好好写一篇文章吧. MySQL的四种事务隔离级别 https://www.cnblogs.com/huanongying/p/70215 ...

  10. node.js常用命令

    安装node 验证是否安装node $node -v $npm -v npm node package manager , Node 的包管理器 安装 包 # 安装到当前目录 $ npm instal ...