Android的远程服务回调 [英] Android remote service callbacks

查看:249
本文介绍了Android的远程服务回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(我有一个远程所使用的多个客户端应用程序服务与AIDL接口。我想一个异步方法添加到该花一些时间调用的接口,但我需要的解决方案是安全,这意味着只有我的应用程序能够与服务通信的客户端应用程序具有相同签名作为服务的应用程序签名。目前,该应用程序只绑定到服务,并调用一个单一的界面方法来执行各种操作。

(I have a remote service with an AIDL interface that is used by several client apps. I would like to add an asynchronous method to the interface for calls that take some time, but I need the solution to be secure, meaning that only my applications can communicate with the service. The client applications are signed with the same signature as the service app. Currently the apps just bind to the service and call a single interface method to perform various operations.

一种选择是广播从服务的意图,当操作完成,并在客户端应用程序使用一个BroadcastReceiver,但(问题1 )可以这样做的方式,只有我保证应用程序可以收到意图是什么? setPackage()似乎要做到这一点,但我需要支持姜饼的设备,这似乎是根据这里的答案排除方法:的 setPackage的意图姜饼

One option is broadcasting an Intent from the service when the operation is complete and using a BroadcastReceiver in the client application, but (Question #1) can this be done in a way that ensures only my apps can receive the Intent? setPackage() seems to do this, but I need to support Gingerbread devices, which seems to rule out that approach according to the answer here: setPackage for intent in gingerbread

这样看来,我需要添加第二个.aidl接口与回调接口,该服务使用,由客户端实现的。我已经看到,使用监听器在这里的例子,但我不知道的区别是相对于客户端只是路过第二界面对象作为参数(从这个答案用在的iScript / IScriptResult例如什么:服务回拨到安卓活动)

So it seems I need to add a second .aidl interface with the callback interface for the service to use, implemented by the client. I have seen examples that use listeners here, but I am not sure what the difference is versus the client just passing in the second interface object as an argument (as used in the IScript / IScriptResult example from this answer: Service call backs to activity in android)

问题#2 ,什么是这里使用侦听与一个回调方法的好处?

Question #2, what is the benefit of using a listener here vs. a callback method?

推荐答案

一个回调方法/监听器是正确的事情。 (由于CommonsWare说,这是pretty的同样的事情)。我会说这是不是与BroadcastReceivers摆弄周围要简单得多,因为你已经在使用AIDL。

A callback method/listener is the right thing to do. (As CommonsWare says, it's pretty much the same thing). I would say it's much simpler than fiddling around with BroadcastReceivers, since you're already using aidl.

事情是这样的:

IAsyncThing.aidl:

package com.my.thingy;

import com.my.thingy.IAsyncThingListener;

interface IAsyncThing {
  void doSomething(IAsyncThingListener listener);
}

IAsyncThingListener.aidl:

package com.my.thingy;

import com.my.thingy.IAsyncThingListener;

interface IAsyncThingListener {
  void onAsyncThingDone(int resultCodeIfYouLike);
}

您可以强制执行,只有您的应用程序可以绑定到该服务通过使用你的服务的签名级别的权限(见这里的服务权限注:<一href="http://developer.android.com/guide/topics/security/permissions.html">http://developer.android.com/guide/topics/security/permissions.html).具体做法是:

You can enforce that only your apps can bind to the service by using a signature-level permission on your service (see the note on 'service permissions' here: http://developer.android.com/guide/topics/security/permissions.html). Specifically:

  • 在声明权限在服务的的Andr​​oidManifest.xml 。确保它是签名的水平。
  • 添加该权限在服务标记
  • 在所有其他应用程序,使用使用-许可来使用它。
  • Declare a permission in your service's AndroidManifest.xml. Ensure it is signature level.
  • Add that permission in your service tag
  • In all the other apps, use uses-permission to use it.

几个其他的事情要牢记:

A couple of other things to bear in mind:

  • 在来电者,你需要继承 IAsyncThingListener.Stub 。你调用的应用程序code可能已经被继承别的东西,所以这意味着你必须使用一个额外的(可能是内部)类接收完成通知。我提到这一点,只是因为这可能是回答问题2 - 我不完全理解
  • 如果该服务可能是从呼叫者不同的过程,每个人都应该登记为其他使用 IBinder.linkToDeath 的死亡通知。
  • In the caller, you'll need to subclass IAsyncThingListener.Stub. Your calling application code may already be subclassing something else, so that means you'd have to use an extra (probably inner) class to receive the completion notification. I mention this only because this might be the answer to question #2 - which I don't fully understand.
  • If the service is potentially in different processes from the caller, each should register for death notification of the other using IBinder.linkToDeath.

这篇关于Android的远程服务回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆