How to run android program without open the app?

后端 未结 3 709
忘了有多久
忘了有多久 2021-01-28 04:56

My app can be opened by NFC, and then use the information in the tag to do some processing, the processing is around 5 seconds.

If my app is opened by NFC, I don\'t want

3条回答
  •  盖世英雄少女心
    2021-01-28 05:19

    If I am getting you right, you want to start some operations in the background as soon as the app gets started via NFC. The point is that, as soon as you actually start your app and the onCreate method of an activity gets called, your application will enter foreground and will become visible.

    Assuming, you do not necessarily need to start an activity but some process in your application via NFC, the best solution is to outsource the code that should run without bringing the app into foreground into a BackgroundService. Set up a BroadcastReceiver onto the NFC-events you want to get informed about. As soon as it receives a notification, this receiver can then start your background service.

    EDIT: grexter89 is right, I am sorry for my non-realizable hint. The intents provided by an NFC event can only be processed within an activity! Well, I am sure this is not the answer you want to here but I guess that this limitation is based upon some security/privacy issues. If the approach mentioned above was possible, NFC events could start processes without the consent and confirmation of the user as soon as the user, maybe accidentally, gets into contact with an NFC field. This is the use case you would like to use for your application but, however, these circumstances could be easily misused.

    Regarding these points you should rethink if you actually want your activity to be invisible. Given the activity-only-limitation from Google and the above reasons (=> activation by accident, lack of user awareness) I would implement an approach with a visible Activity that shortly displays a hint to the user.

提交回复
热议问题