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

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

829b54cce87aeba4150d6ddeb6b99975.png

e3203a482f0ba52eae00635bda1dbe6c.png

服务端:

//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文件

package="com.itheima.myaidl.server"

android:versionCode="1"

android:versionName="1.0" >

android:minSdkVersion="14"

android:targetSdkVersion="19" />

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

android:name="com.itheima.myaidl.server.MainActivity"

android:configChanges="locale|layoutDirection"

android:theme="@android:style/Theme.Light.NoTitleBar"

android:screenOrientation="portrait">

//客户端

//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();

}

}

运行结果截图:

8b5a8e84db9d0cb5b00653d73ced6fc4.png

0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值