android aidl 远程接口调用,Android aidl接口远程调用

CREATOR = new Parcelable.Creator{

public Rect createFromParcel(Parcel in) {

return new Rect(in);

}

public Rect[] newArray(int size) {

return new Rect[size];

}

};

public Rect() {    }

private Rect(Parcel in) {

readFromParcel(in);

}

public void writeToParcel(Parcel out) {

out.writeInt(left);

out.writeInt(top);

out.writeInt(right);

out.writeInt(bottom);

}

public void readFromParcel(Parcel in) {

left = in.readInt();

top = in.readInt();

right = in.readInt();

bottom = in.readInt();

}

}

Here is Rect.aidl for this example

示例的Rect.aidl

1. package android.graphics;

2. // Declare Rect so AIDL can find it and knows that it implements

3. // the parcelable protocol.

4. parcelable Rect;

package android.graphics;

// Declare Rect so AIDL can find it and knows that it implements

// the parcelable protocol.

parcelable Rect;

The marshalling in the Rect class is pretty simple. Take a look at the other methods on Parcel to see the other kinds of values you can write to a Parcel.

Rect类中的伪装是相当简单的。仔细看看Parcel中的其他方法,你会看到其他各种值你都可以写进Parcel.

Java 代码

1. Warning: Don't forget the security implications of receiving data from other processes. In this case, the rect will read four numbers from the parcel, but it is up to you to ensure that these are within the acceptable range of values for whatever the caller is trying to do. See Security and Permissions in Android for more on how to keep your application secure from malware.

2.

3. 警告:不要忽视从其他进程接收数据时的安全性考虑。在本例中,rect将从parcel中读四个数字,而你的工作则是确保这些都在可接受的值得范围内而不管调用者想要干什么。AndRoid中的安全和访问许可中有更多关于如何确保应用程序安全的信息。

Warning: Don't forget the security implications of receiving data from other processes. In this case, the rect will read four numbers from the parcel, but it is up to you to ensure that these are within the acceptable range of values for whatever the caller is trying to do. See Security and Permissions in Android for more on how to keep your application secure from malware.

警告:不要忽视从其他进程接收数据时的安全性考虑。在本例中,rect将从parcel中读四个数字,而你的工作则是确保这些都在可接受的值得范围内而不管调用者想要干什么。AndRoid中的安全和访问许可中有更多关于如何确保应用程序安全的信息。

调用一个IPC方法

Here are the steps a calling class should make to call your remote interface:

调用类调用远程接口的步骤:

1.Declare a variable of the interface type that your .aidl file defined.

Java 代码

1.声明一个接口类型的变量,该接口类型在.aidl文件中定义。

2.Implement ServiceConnection.

2.实现ServiceConnection。

3.Call ApplicationContext.bindService(), passing in your ServiceConnection implementation.

3.调用ApplicationContext.bindService(),并在 ServiceConnection实现中进行传递.

4.In your implementation of ServiceConnection.onServiceConnected(), you will receive an IBinder instance (called service).

Call YourInterfaceName.Stub.asInterface((IBinder)service) to cast the returned parameter to YourInterface type.

4.在ServiceConnection.onServiceConnected()实现中,你会接收一个IBinder实例(被调用的Service). 调用

YourInterfaceName.Stub.asInterface((IBinder)service) 将参数转换为YourInterface类型。

5.Call the methods that you defined on your interface. You should always trap DeadObjectException exceptions, which are

thrown when the connection has broken; this will be the only exception thrown by remote methods.

5.调用接口中定义的方法。 你总会捕捉到DeadObjectException异常,该异常在连接断开时被抛出。它只会被远程方法抛出。

6.To disconnect, call ApplicationContext.unbindService() with the instance of your interface.

6. 断开连接,调用接口实例中的 ApplicationContext.unbindService()

A few comments on calling an IPC service:

调用IPC服务需要注意几点:

Java 代码

1. .Objects are reference counted across processes.

2.

3. .You can send anonymous objects as method arguments.

4. . 匿名对象可以通过方法参数发送。

.Objects are reference counted across processes.

.You can send anonymous objects as method arguments.

.匿名对象可以通过方法参数发送。

Here is some sample code demonstrating calling an AIDL-created service, taken from the Remote Activity sample in the ApiDemos project.

下面的代码展示了在ApiDemos项目从远程Activity例子中调用AIDL创建Service的过程。

Java 代码

1. public class RemoteServiceBinding extends Activity{

2.     IRemoteService mService = null;

3.     Button mKillButton;

4.     private boolean mIsBound;

5.     @Override

6.     protected void onCreate(Bundle icicle)    {

7.         super.onCreate(icicle);

8.         setContentView(R.layout.remote_service_binding);

9.         // Watch for button clicks.

10.         Button button = (Button)findViewById(R.id.bind);

11.         button.setOnClickListener(mBindListener);

12.         button = (Button)findViewById(R.id.unbind);

13.         button.setOnClickListener(mUnbindListener);

14.         mKillButton = (Button)findViewById(R.id.kill);

15.         mKillButton.setOnClickListener(mKillListener);

16.         mKillButton.setEnabled(false);

17.     }

18.     private ServiceConnection mConnection = new ServiceConnection()    {

19.         public void onServiceConnected(ComponentName className, IBinder service)        {

20.             // This is called when the connection with the service has been

21.             // established, giving us the service object we can use to

22.             // interact with the service.  We are communicating with our

23.             // service through an IDL interface, so get a client-side

24.             // representation of that from the raw service object.

25.             mService = IRemoteService.Stub.asInterface((IBinder)service);

26.             mKillButton.setEnabled(true);

27.             // As part of the sample, tell the user what happened.

28.             NotificationManager nm = (NotificationManager)

29.                 getSystemService(NOTIFICATION_SERVICE);

30.             nm.notifyWithText(R.string.remote_service_connected,

31.                       getText(R.string.remote_service_connected),

32.                       NotificationManager.LENGTH_SHORT,

33.                       null);

34.         }

35.         public void onServiceDisconnected(ComponentName className)        {

36.             // This is called when the connection with the service has been

37.             // unexpectedly disconnected -- that is, its process crashed.

38.             mService = null;

39.             mKillButton.setEnabled(false);

40.             // As part of the sample, tell the user what happened.

41.             NotificationManager nm = (NotificationManager)

42.                     getSystemService(NOTIFICATION_SERVICE);

43.             nm.notifyWithText(R.string.remote_service_disconnected,

44.                       getText(R.string.remote_service_disconnected),

45.                       NotificationManager.LENGTH_SHORT,

46.                       null);

47.         }

48.     };

49.     private OnClickListener mBindListener = new OnClickListener()    {

50.         public void onClick(View v)        {

51.             // Establish a connection with the service, by its class name.

52.             bindService(new Intent(RemoteServiceBinding.this,

53.                         RemoteService.class),

54.                     null, mConnection, Context.BIND_AUTO_CREATE);

55.             mIsBound = true;

56.         }

57.     };

58.     private OnClickListener mUnbindListener = new OnClickListener()    {

59.         public void onClick(View v)        {

60.             if (mIsBound) {

61.                 // Detach our existing connection.

62.                 unbindService(mConnection);

63.                 mKillButton.setEnabled(false);

64.                 mIsBound = false;

65.             }

66.         }

67.     };

68.     private OnClickListener mKillListener = new OnClickListener()    {

69.         public void onClick(View v)        {

70.             // To kill the process hosting our service, we need to know its

71.             // PID.  Conveniently our service has a call that will return

72.             // to us that information.

73.             if (mService != null) {

74.                 int pid = -1;

75.                 try {

76.                     pid = mService.getPid();

77.                 } catch (DeadObjectException ex) {

78.                     // Recover gracefully from the process hosting the

79.                     // server dying.

80.                     // Just for purposes of the sample, put up a notification.

81.                     NotificationManager nm = (NotificationManager)

82.                         getSystemService(NOTIFICATION_SERVICE);

83.                     nm.notifyWithText(R.string.remote_call_failed,

84.                               getText(R.string.remote_call_failed),

85.                               NotificationManager.LENGTH_SHORT,

86.                               null);

87.                 }

88.                 if (pid > 0) {

89.                     // Go away!

90.                     Process.killProcess(pid);

91.                 }

92.             }

93.         }

94.     };

95. }

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值