标签:
package com.magc.rpc;
interface IPlayer
{
void setName(String name);
void addFile(String f_name);
String ToString();
}
/*
* This file is auto-generated. DO NOT MODIFY.
* Original file: F:\\work\\Android_App\\MyRPCService\\src\\com\\magc\\rpc\\IPlayer.aidl
*/
package com.magc.rpc;
public interface IPlayer extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements com.magc.rpc.IPlayer
{
private static final java.lang.String DESCRIPTOR = "com.magc.rpc.IPlayer";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an com.magc.rpc.IPlayer interface,
* generating a proxy if needed.
*/
public static com.magc.rpc.IPlayer asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = (android.os.IInterface)obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.magc.rpc.IPlayer))) {
return ((com.magc.rpc.IPlayer)iin);
}
return new com.magc.rpc.IPlayer.Stub.Proxy(obj);
}
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_setName:
{
data.enforceInterface(DESCRIPTOR);
java.lang.String _arg0;
_arg0 = data.readString();
this.setName(_arg0);
reply.writeNoException();
return true;
}
case TRANSACTION_addFile:
{
data.enforceInterface(DESCRIPTOR);
java.lang.String _arg0;
_arg0 = data.readString();
this.addFile(_arg0);
reply.writeNoException();
return true;
}
case TRANSACTION_ToString:
{
data.enforceInterface(DESCRIPTOR);
java.lang.String _result = this.ToString();
reply.writeNoException();
reply.writeString(_result);
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements com.magc.rpc.IPlayer
{
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote)
{
mRemote = remote;
}
public android.os.IBinder asBinder()
{
return mRemote;
}
public java.lang.String getInterfaceDescriptor()
{
return DESCRIPTOR;
}
public void setName(java.lang.String name) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeString(name);
mRemote.transact(Stub.TRANSACTION_setName, _data, _reply, 0);
_reply.readException();
}
finally {
_reply.recycle();
_data.recycle();
}
}
public void addFile(java.lang.String f_name) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeString(f_name);
mRemote.transact(Stub.TRANSACTION_addFile, _data, _reply, 0);
_reply.readException();
}
finally {
_reply.recycle();
_data.recycle();
}
}
public java.lang.String ToString() 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_ToString, _data, _reply, 0);
_reply.readException();
_result = _reply.readString();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
}
static final int TRANSACTION_setName = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
static final int TRANSACTION_addFile = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
static final int TRANSACTION_ToString = (android.os.IBinder.FIRST_CALL_TRANSACTION + 2);
}
public void setName(java.lang.String name) throws android.os.RemoteException;
public void addFile(java.lang.String f_name) throws android.os.RemoteException;
public java.lang.String ToString() throws android.os.RemoteException;
}
package com.magc.rpc;
import android.os.RemoteException;
import android.util.Log;
import com.magc.rpc.IPlayer.Stub;
/**
*
* @author magc
* 实现IPlayer接口类中的静态内部抽象类,即实现IPlayer接口方法
* 将来供其它应用程序远程调用执行方法
*/
public class MyPlayer extends Stub {
private String name="";
@Override
public void addFile(String fName) throws RemoteException {
System.out.println("add file ...");
}
@Override
public void setName(String name) throws RemoteException {
this.name = name;
Log.i("magc", "setName--"+name);
}
public String ToString()
{
String str = "MyPlayer--"+name;
Log.i("magc", "MyPlayer--"+name);
return str;
}
}
package com.magc.rpc;
import com.magc.rpc.IPlayer.Stub;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
/**
*
* @author magc
* 此服务类作为一个代理角色,供其它应用程序绑定,并返回接口实例
*
* 可看作是代理模式的应用
*/
public class MyService extends Service {
private Stub player = new MyPlayer();
@Override
public IBinder onBind(Intent arg0) {
return player;
}
@Override
public void onCreate() {
super.onCreate();
}
}
package com.magc.rpc;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
/**
*
* @author magc
* 作为一个客户端通过绑定MyService服务,实现远程调用IPlayer接口方法
*
*/
public class MainActivity extends Activity {
private String ACTION="com.magc.rpc.action.MYSERVICE";
private IPlayer player;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent();
intent.setAction(ACTION);
//绑定MyService服务
bindService(intent, conn, BIND_AUTO_CREATE);
}
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
}
/**
* 绑定MyService服务后,返回IPlayer接口,进而调用该接口方法
*/
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("magc", "bind service .....");
player = IPlayer.Stub.asInterface(service);
if(player!=null)
{
try {
player.setName("magc");
player.ToString();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.magc.rpc"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="9" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService">
<intent-filter>
<action android:name="com.magc.rpc.action.MYSERVICE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</service>
</application>
</manifest>
理解Android系统的进程间通信原理(二)----RPC机制
标签:
原文地址:http://www.cnblogs.com/xiaoxiaoshen/p/5212345.html