Android HCE service

守給你的承諾、 提交于 2019-12-13 02:27:59

问题


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?


回答1:


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.

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.

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);
}

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.



来源:https://stackoverflow.com/questions/25176181/android-hce-service

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