安卓 HCE 服务 [英] Android HCE service

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

问题描述

我有一个 HCE 服务,它可以与 NFC 阅读器相互发送数据,但我想知道是否可以指定我的服务的启动设置.我想指定只有在应用程序正在运行时才能调用我的服务.如果应用程序关闭,则不应调用该服务.这是可能的还是打算始终可以调用该服务?

I have a HCE service which sends data to and from a NFC reader but I was wondering if I can specify the launch settings of my service. I want to specify that my service can only be called if the app is running. If the app is closed the service shouldn't be called. Is this possible or was it intended that the service can always be called?

推荐答案

目前您只能指定是否应考虑将 HCE 服务用于从锁屏进行卡模拟,或者仅当用户通过锁屏时(请参阅 此处.无法(直接)指定HCE 服务仅应在您应用的 Activity 处于前台时使用.

Currently you can only specify if the HCE service should be considered for card emulation from the lock-screen or only if the user passed the lock screen (see here. It's not (directly) possible to specify that the HCE service should only be used while an activity of your app is in the foreground.

使用服务作为 HCE 事件的目标(而不是其他 NFC 事件的情况下的活动)背后的整个想法是 HCE 功能应该可以随时访问并且不需要活动在前台.

The whole idea behind using a service as the target for HCE events (and not an activity as it is the case for other NFC events) is that the HCE functionbality should be accessible at any time and does not require an activity to be in the foreground.

为了限制 HCE 服务的可用性,您可以在应用的共享首选项中设置一个标志(请参阅这个问题 为什么共享首选项将是首选方法)指示是否允许服务执行某些操作.然后,您可以在 HCE 服务中查询该标志,并决定应通过 HCE 提供哪些功能.

What you could do to limit the availability of your HCE service is to set a flag in your app's shared preferences (see this question on why shared preferences would be the prefered approach) that indicates if the service is allowed to perform certain actions or not. You could then query that flag within the HCE service and decide what functionality should be available over HCE.

或者,您可以禁用整个服务组件(请参阅此问题/答案)并仅在您的活动在前台:

Alternatively, you could disable the whole service component (see this question/answer) and only enable the service while your activity is in the foreground:

public void onResume() {
    super.onResume();

    PackageManager pm = getPackageManager();
    pm.setComponentEnabledSetting(new ComponentName(this, "com.example.app.HceService"),
                                  PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                                  PackageManager.DONT_KILL_APP);
}

public void onPause() {
    super.onPause();

    PackageManager pm = getPackageManager();
    pm.setComponentEnabledSetting(new ComponentName(this, "com.example.app.HceService"),
                                  PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                                  PackageManager.DONT_KILL_APP);
}

请注意,我建议您的 HCE 服务仍然响应某些命令(例如应用程序选择等),并且您只阻止安全/隐私相关命令.

Note that I would suggest that your HCE service still responds to certain commands (e.g. application selection, etc.) and that you only block security/privacy relevant commands.

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

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