Service个人笔记2(aidl)

      这篇博文讲的是绑定远程服务(aidl)


远程服务与本地服务的区别


远程服务调用可以跨进程调用,通俗的说就是A应用可以调用B应用的服务(前提:已经实现了远程调用的规则)


实现远程服务的思想可以这样理解




我们接下来看一下具体代码是如何实现的

-------------------------------------------------------------------------------------------------首先提供B应用(远程服务提供者)的代码-------------------------------------------------------------------------------------------------


该应用的主要部分


AlipayService.java

IService.aidl

IService.java     (这个是IService.aidl 建立以后adt自动生成的)

(activity没有什么关键内容就不贴代码了)



AlipayService.java

package com.itheima.alipayservice;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.widget.Toast;

public class AliPayService extends Service {

	@Override
	public IBinder onBind(Intent intent) {
		Toast.makeText(getApplicationContext(), "支付宝服务已绑定", 0).show();
		return new MyIBinder();
	}

	@Override
	public boolean onUnbind(Intent intent) {
		Toast.makeText(getApplicationContext(), "支付宝服务已解绑", 0).show();
		return super.onUnbind(intent);
	}

	@Override
	public void onCreate() {
		Toast.makeText(getApplicationContext(), "支付宝服务已创建", 0).show();
		super.onCreate();
	}

	@Override
	public void onDestroy() {
		Toast.makeText(getApplicationContext(), "支付宝服务已销毁", 0).show();
		super.onDestroy();
	}

	private String pay() {
		return "支付宝服务提供远程支付服务";
	}

	// 这个地方与绑定本地服务有所区别,因为你建立了aidl文件,必须统一规则。
	// 所以自定义的MyIBinder必须实现 自动生成的IService.java中的内部类
	private class MyIBinder extends IService.Stub {
		// Stub 已经 extends Binder 实现了IService接口
		// public static abstract class Stub extends android.os.Binder
		// implements com.itheima.alipayservice.IService
		@Override
		public String callPay() throws RemoteException {
			String result = pay();
			return result;
		}

	}

}

IService.aidl

package com.itheima.alipayservice;

interface IService{
	String callPay();
}

IService.java

/*
 * This file is auto-generated.  DO NOT MODIFY.
 * Original file: E:\\java\\Documents and Settings\\margi\\workspace\\42_itheima_alipayservice\\src\\com\\itheima\\alipayservice\\IService.aidl
 */
package com.itheima.alipayservice;
public interface IService extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements com.itheima.alipayservice.IService
{
private static final java.lang.String DESCRIPTOR = "com.itheima.alipayservice.IService";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
 * Cast an IBinder object into an com.itheima.alipayservice.IService interface,
 * generating a proxy if needed.
 */
public static com.itheima.alipayservice.IService asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.itheima.alipayservice.IService))) {
return ((com.itheima.alipayservice.IService)iin);
}
return new com.itheima.alipayservice.IService.Stub.Proxy(obj);
}
@Override public android.os.IBinder asBinder()
{
return this;
}
@Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
{
switch (code)
{
case INTERFACE_TRANSACTION:
{
reply.writeString(DESCRIPTOR);
return true;
}
case TRANSACTION_callPay:
{
data.enforceInterface(DESCRIPTOR);
java.lang.String _result = this.callPay();
reply.writeNoException();
reply.writeString(_result);
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements com.itheima.alipayservice.IService
{
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote)
{
mRemote = remote;
}
@Override public android.os.IBinder asBinder()
{
return mRemote;
}
public java.lang.String getInterfaceDescriptor()
{
return DESCRIPTOR;
}
@Override public java.lang.String callPay() throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
java.lang.String _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_callPay, _data, _reply, 0);
_reply.readException();
_result = _reply.readString();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
}
static final int TRANSACTION_callPay = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
}
public java.lang.String callPay() throws android.os.RemoteException;
}



------------------------------------------------------------------------------------------A应用(远程服务的调用者)-------------------------------------------------------------------------



MainActivity.java


package com.itheima.alipayuser;

import com.itheima.alipayservice.IService;

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

public class MainActivity extends Activity {

	private Intent intent;
	private IService iService;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		intent = new Intent();
		intent.setAction("com.itheima.alipay");
	}

	public void bind(View view) {
		this.bindService(intent, new MyConn(), Context.BIND_AUTO_CREATE);
	}
	
	//调用远程服务
	public void call(View view) {
		try {
		    //执行远程服务中的方法
			String result = this.iService.callPay();
			Toast.makeText(getApplicationContext(), result, 0).show();
		} catch (RemoteException e) {
			//调用远程服务的方法时会抛出remoteexception
			e.printStackTrace();
		}
	}

	private class MyConn implements ServiceConnection {

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			
			//得到远程服务
			iService = IService.Stub.asInterface(service);
			//用其自己的方法实现类型转换

		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub

		}

	}
}

IService.aidl

内容是一样的就不贴代码了

IService.java


必须要注意的是,A应用中(服务调用者)包含 .aidl 文件的package必须和 B应用(服务提供者)保持一致,最好直接复制


最后可以说一下,就是创建  .aidl 文件的时候的步骤


这个步骤你首先可以把它当成一个绑定本地的服务来写,还是把IService定义成一个接口,写成IService.java 然后修改文件后缀名  IService.aidl

最后根据aidl的语法 修改其中的内容。这样思路可能更清晰一些。

相关文章
相关标签/搜索