Android HCE服务 [英] Android HCE service

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

问题描述

我有一个HCE服务,该服务可以向NFC读取器发送数据,也可以从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服务仅应在应用程序的活动处于前台时使用.

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.

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

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