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

查看:24
本文介绍了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.

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

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 示例中所使用:服务回调到android中的活动)

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 所说,这几乎是一样的).我会说这比摆弄 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);
}

您可以通过对您的服务使用签名级权限来强制只有您的应用可以绑定到该服务(请参阅此处关于服务权限"的说明: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:

  • 在服务的 AndroidManifest.xml 中声明权限.确保它是 signature 级别.
  • 在您的 service 标签中添加该权限
  • 在所有其他应用中,使用 uses-permission 来使用它.
  • 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.

要记住的其他几件事:

  • 在调用方中,您需要对 IAsyncThingListener.Stub 进行子类化.您的调用应用程序代码可能已经子类化了其他东西,因此这意味着您必须使用额外的(可能是内部的)类来接收完成通知.我提到这一点只是因为这可能是问题 #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天全站免登陆