Passing information to a HostApduService from another Context

浪子不回头ぞ 提交于 2019-11-28 13:49:47

I think this question comes down to another question:

How would you store (persistent) data within a bound service?

The HCE service is a bound service, thus it is only guaranteed to run while its calling context exists (see here). As a result, whatever data you store within the service (→ variables that exist in RAM) will only exist for the lifetime of the service and will no longer be available after the service is restarted.

Consequently, binding to the service and setting some values within the service would not reliably work, even if you could bind to the HCE service. The same issue applies to data that you broadcast to the service.

A similar problem exists with storing values in static fields. Those fields only exist as long as their declaring context (i.e. the class they are declared in) is loaded in the virtual machine's memory. As the VM can be killed at any time when the service is not used and no activity of the app is displayed in the foreground, you can't be sure that data you put into static fields survives the whole duration that you need it.

So the only reliable method to store your data is to use persistent storage technology, which is to either use the SharedPreferences mechanism, to use a content provider (that's backed by a database on persistent storage) or to directly store data to files. I guess the best you could do is to store the data on the files system encrypted. However, this brings you to another issue: How to (securely) store the encryption key. But you might be able to adeqately solve that isse using the Android key store (see here and here).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!