android - 如何构建我的应用程序以在后台运行

标签 android

我是 Android 新手,需要一些入门建议。
我想构建一个应用程序,当用户遇到一些热点情况时,它会显示出来。

我所说的热点情况是指:

  • GPS/小区坐标在已知区域;
  • 检测到已知的蓝牙设备;
  • 检测到已知 Wi-Fi 网络;
  • 天气信息有变化;

我看到有东西在后台运行,当其中一个子句命中时,它会触发并打开应用程序。

  • 如何开始?
  • 如何确保我的应用程序不会被关闭?

正如我在某处读到的那样,如果发生内存不足或消耗过多,Android 操作系统将终止应用程序,并且我的应用程序会消耗大量资源,反复测量/检查以查看情况是否发生变化。

问候,
奔腾10

最佳答案

您需要为在后台运行的应用程序部分使用服务

您可能会找到 Application Fundamentals document in the Android Developer Documentation有帮助。它是这样描述服务的:

A service doesn't have a visual user interface, but rather runs in the background for an indefinite period of time. For example, a service might play background music as the user attends to other matters, or it might fetch data over the network or calculate something and provide the result to activities that need it.

在您的情况下,您可能会找到 LocationManager Service有帮助。这是一个系统服务,您可以使用它根据 GPS 位置通知您的应用程序。

但是,我认为您必须编写自己的服务来监控 Wi-Fi、蓝牙和天气。

您可以使用 the AlarmManager Service让您的服务以特定的时间间隔执行特定的任务。

关于android - 如何构建我的应用程序以在后台运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2005166/

相关文章:

android - android ListView 中的行图像更改?

android - "AndroidApkSigner"任务没有为所需参数 "KeyPass"赋值

android - 是否可以在不使用 android market 的情况下在 Android 中进行自动更新?

javascript - html javascript 类 ="highslide"在 android 浏览器上不起作用

c# - 遍历 ICollection

android - 如何将Alpha设置为android Button

Android 不会停止 GPS Intent

android - CoordinatorLayout 内的水平 RecyclerView

android - 如何在 Android 的 View.Canvas 上缩放位图?

android - 我们如何键入 Sqlite 查询以创建具有属性的列的顺序真的很重要吗