Android AIDL实现进程间通讯IPC

import com.example.aidlserver.aidl.DataService;

import android.app.Service;

import android.content.Intent;

import android.os.IBinder;

import android.os.RemoteException;

public class TestService extends Service {

@Override

public IBinder onBind(Intent intent) {

return binder;

}

private final DataService.Stub binder = new DataService.Stub() {

@Override

public int getData(String type) throws RemoteException {

return 5;

}

@Override

public String getTime() throws RemoteException {

return “2016-01-23”;

}

};

}

③AndroidManifest.xml声明Service,采用隐式Intent供Client调用:

2.AIDLClient

①将AIDLServer中的aidl文件和其包复制到AIDLClient下。

②通过隐式Intent和ServiceConnection 绑定Service之后就可以使用AIDLServer中的数据接口:

package com.example.aidlclient;

import com.example.aidlserver.aidl.DataService;

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 android.view.View;

import android.widget.Button;

import android.widget.Toast;

public class MainActivity extends Activity {

private Button btnBind, btnUnBind, btnData, btnTime;

private DataService dataService;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

btnBind = (Button) findViewById(R.id.btnBind);

btnBind.setOnClickListener(new MyOnClickListener());

btnUnBind = (Button) findViewById(R.id.btnUnBind);

btnUnBind.setOnClickListener(new MyOnClickListener());

btnData = (Button) findViewById(R.id.btnData);

btnData.setOnClickListener(new MyOnClickListener());

btnTime = (Button) findViewById(R.id.btnTime);

btnTime.setOnClickListener(new MyOnClickListener());

}

ServiceConnection serviceConnection = new ServiceConnection() {

@Override

public void onServiceDisconnected(ComponentName name) {

Log.v(“ZMS”, “ServiceConnection: onServiceDisconnected”);

}

@Override

public void onServiceConnected(ComponentName name, IBinder service) {

dataService = DataService.Stub.asInterface(service);

Log.v(“ZMS”, “ServiceConnection: onServiceConnected”);

}

};

class MyOnClickListener implements View.OnClickListener {

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.btnBind:

// Intent intent = new Intent(DataService.class.getName());

Intent intentBind = new Intent(

“com.example.aidlserver.aidl.DataService”);

boolean bindSuccess = bindService(intentBind,

serviceConnection, Context.BIND_AUTO_CREATE);

Toast.makeText(MainActivity.this, “bindSuccess:” + bindSuccess,

Toast.LENGTH_SHORT).show();

break;

case R.id.btnUnBind:

if (dataService != null) {

unbindService(serviceConnection);

} else {

Log.e(“ZMS”, “dataService is NULL”);

}

break;

case R.id.btnData:

try {

int data = dataService.getData(“”);

Toast.makeText(MainActivity.this, “” + data,

Toast.LENGTH_SHORT).show();

} catch (RemoteException e) {

e.printStackTrace();

}

break;

case R.id.btnTime:

try {

String time = dataService.getTime();

Toast.makeText(MainActivity.this, “” + time,

Toast.LENGTH_SHORT).show();

} catch (RemoteException e) {

e.printStackTrace();

}

break;

default:

break;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值