运行 应用被杀死时的一些代码

Run some code when app is killed

我正在尝试开发一个与 ibeacons 交互的移动应用程序!

我正在为 iPhone 和 android 开发此应用程序!但是我有问题!当我开发 iphone 应用程序时,一切正常, 即使我的应用程序被终止,我也会收到通知!

但是在 android 我不知道我该如何开发它! 如果我的应用程序在后台运行,它可以运行,但如果我终止我的应用程序,则什么也不会发生!

你有没有想过 运行 我的代码,即使应用程序被杀死?

提前致谢!

您可以使用服务。即使你的 activity 被杀,它仍然会工作。 https://developer.android.com/guide/components/services.html

如果您想要 运行 在后台运行一些代码,即使您的应用程序已被终止,您应该使用 services。来自 google 文档:

A Service is an application component that can perform long-running operations in the background, and it does not provide a user interface. Another application component can start a service, and it continues to run in the background even if the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service can handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.

如果您的应用 运行 在预定的时间运行,您可以使用 JobScheduler 或 AlarmManger。 JobScheduler 和 AlarmManager 与服务一起工作。