Android中利用AIDL机制调用远程服务


服务端: //CalculateInterface.aidl
package com.itheima.aidl.calculate; interface CalculateInterface {
double doCalculate(double a, double b);
} //CalculateService.java
package com.itheima.myaidl.server; import com.itheima.aidl.calculate.CalculateInterface; import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log; public class CalculateService extends Service{
private final CalculateInterface.Stub mBinder = new CalculateInterface.Stub() {
@Override
public double doCalculate(double a, double b) throws RemoteException {
return a+b;
}
}; @Override
public IBinder onBind(Intent intent) {
Log.i("test","onBind...");
return mBinder;
} @Override
public boolean onUnbind(Intent intent) {
Log.i("test","onUnbind...");
return super.onUnbind(intent);
} @Override
public void onCreate() {
super.onCreate();
Log.i("test","onCreate...");
} @Override
public void onDestroy() {
super.onDestroy();
Log.i("test","onDestroy...");
}
} //服务端manifast文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.myaidl.server"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.itheima.myaidl.server.MainActivity"
android:configChanges="locale|layoutDirection"
android:theme="@android:style/Theme.Light.NoTitleBar"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.itheima.myaidl.server.CalculateService">
<intent-filter>
<action android:name="com.itheima.myaidl.server.CalculateService" />
</intent-filter>
</service>
</application> </manifest> //客户端
//MainActivity.java package com.itheima.myaidl.client; import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log; import com.itheima.aidl.calculate.CalculateInterface; public class MainActivity extends Activity {
private CalculateInterface mService; private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i("test","service disconnected...");
mService = null;
} @Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("test","service connected...");
mService = CalculateInterface.Stub.asInterface(service); //获取接口实例
}
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //绑定远程服务
Bundle bundle = new Bundle();
Intent intent = new Intent();
intent.putExtras(bundle);
intent.setAction("com.itheima.myaidl.server.CalculateService");
bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
} //TODO activity加载完毕时回调此方法
@Override
public void onWindowFocusChanged(boolean hasFocus) {
if(hasFocus){
try{
double result = mService.doCalculate(1, 2);
Log.i("test","result===>"+result);
}catch(RemoteException e){
e.printStackTrace();
}
}
super.onWindowFocusChanged(hasFocus);
} @Override
protected void onDestroy() {
unbindService(mServiceConnection); //解绑远程服务
super.onDestroy();
}
}
运行结果截图:

Android中利用AIDL机制调用远程服务的更多相关文章
- Android 中的AIDL,Parcelable和远程服务
Android 中的AIDL,Parcelable和远程服务 早期在学习期间便接触到AIDL,当时对此的运用也是一撇而过.只到近日在项目中接触到AIDL,才开始仔细深入.AIDL的作用 ...
- Android中利用Handler实现消息的分发机制(三)
在第二篇文章<Android中利用Handler实现消息的分发机制(一)>中,我们讲到主线程的Looper是Android系统在启动App的时候,已经帮我们创建好了,而假设在子线程中须要去 ...
- 浅析Android中的消息机制(转)
原博客地址:http://blog.csdn.net/liuhe688/article/details/6407225 在分析Android消息机制之前,我们先来看一段代码: public class ...
- 浅析Android中的消息机制(转)
在分析Android消息机制之前,我们先来看一段代码: public class MainActivity extends Activity implements View.OnClickListen ...
- 浅析Android中的消息机制-解决:Only the original thread that created a view hierarchy can touch its views.
在分析Android消息机制之前,我们先来看一段代码: public class MainActivity extends Activity implements View.OnClickListen ...
- 浅析Android中的消息机制
在分析Android消息机制之前,我们先来看一段代码: public class MainActivity extends Activity implements View.OnClickListen ...
- Android中的消息机制
在分析Android消息机制之前.我们先来看一段代码: public class MainActivity extends Activity implements View.OnClickListen ...
- Android中Alarm的机制
本次给大家分析的是Android中Alarm的机制所用源码为最新的Android4.4.4.首先简单介绍如何使用Alarm并给出其工作原理,接着分析Alarm和Timer以及Handler在完成定时任 ...
- Android中的Parcel机制 实现Bundle传递对象
Android中的Parcel机制 实现了Bundle传递对象 使用Bundle传递对象,首先要将其序列化,但是,在Android中要使用这种传递对象的方式需要用到Android Parc ...
随机推荐
- mysql中,sleep进程过多,如何解决?
睡眠连接过多,会对mysql服务器造成什么影响? 严重消耗mysql服务器资源(主要是cpu, 内存),并可能导致mysql崩溃. 造成睡眠连接过多的原因? 1. 使用了太多持久连接(个人觉得,在高并 ...
- 使用JQuery统计input和textarea文字输入数量代码
本文主要介绍了jQuery实现统计输入文字个数的方法,需要的朋友可以参考下. HTML部分: <input type="text" value="我是输入的文字&q ...
- js类型转换
1.js中有六种基本类型,分别是object.number.string.Boolean.null.undefined,其中number.string.Boolean为基本类型,有时使用会强制转换成对 ...
- JS导出excel 兼容ie、chrome、firefox
运用js实现将页面中的table导出为excel文件,页面显示如下: 导出的excel文件显示如下: 实现代码: <!DOCTYPE html> <html> <head ...
- SQL server学习
慕课网sql server学习 数据库第一印象:desktop--web server--database server** 几大数据库:sql server.oracle database.DB2. ...
- 基础拾遗------redis详解
基础拾遗 基础拾遗------特性详解 基础拾遗------webservice详解 基础拾遗------redis详解 基础拾遗------反射详解 基础拾遗------委托详解 基础拾遗----- ...
- 在tmux中的vi 上下左右键变为了ABCD等字符
在本机上用vim编辑时,上下左右键没有问题,但是在tmux中确出现ABCD等字符. 原因是在tmux这个终端,默认做了字符转换,网上搜了很多答案,解决问题的设置是: set term=xterm
- Dynamic Programming [Algorithm]
今天学习动态规划01背包问题,从一篇非常不错的文章中学习甚多.转载于此,感谢作者的分享! 原文地址 通过金矿模型介绍动态规划 对于动态规划,每个刚接触的人都需要一段时间来理解,特别是第一次接触的时候总 ...
- BZOJ 1212: [HNOI2004]L语言 [AC自动机 DP]
1212: [HNOI2004]L语言 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1367 Solved: 598[Submit][Status ...
- Spring 事务详解
实现购买股票案例: 一.引入JAR文件: 二.开始搭建分层架构---创建账户(Account)和股票(Stock)实体类 Account: ? 1 2 3 4 5 6 7 8 9 10 11 12 1 ...