关于本地/远程服务、广播接收器和意图服务的结构问题

发布于 2024-10-09 19:18:43 字数 210 浏览 0 评论 0原文

我正在编写一个具有标准活动的 Android 应用程序,但也需要随时监控传入/传出电话和短信。此外,应用程序需要每天向用户通知一次信息,而无需打开活动。它通知用户的信息存储在数据库中,因此不需要与活动进行通信。我已经研究了一个星期,但仍然无法决定如何去做。我的直觉告诉我,我需要一个具有持续运行的广播接收器的远程服务,但我看到的每个远程服务示例都过于复杂。谁能帮助我更好地了解我需要采取哪些步骤?提前致谢。

I'm writing an android app that has a standard activity, but also needs to monitor incoming/outgoing calls and texts at all times. In addition, the app needs to notify users of information once a day without having the activity open. The information it notifies users of is stored in a database, so communication with the activity is not necessary. I've been researching for a week and still can't decide how to go about doing this. My instinct tells me I need a remote service that has a constantly running broadcast receiver, but every remote service example I see is overly complicated. Could anyone help me better understand what steps I need to take? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独患者 2024-10-16 19:18:43

我的直觉告诉我,我需要一个具有持续运行的广播接收器的远程服务

,嗯,不。我这么说是因为:

  • 只有一个应用程序,因此不需要“远程”服务
  • “具有持续运行的广播接收器的服务”是一个非常糟糕的主意,因为用户会用任务杀手攻击您,并且,即使他们不这样做,Android 最终也会终止你的服务,因为这是浪费空间。

使用清单中注册的 BroadcastReceivers 来进行随时可能发生的广播,因为即使您的应用程序没有其他内容正在运行。使用 IntentService 执行这些 BroadcastReceiver 所需的任何“繁重工作”,因为 BroadcastReceiver 获得控制权(在 onReceive() 中))在主应用程序线程上,因此应该做尽可能少的工作。另外,使用 AlarmManagerBroadcastReceiver(或者可能是您的 IntentService)发出信号,为您发出 Notification每日一次的活动。

另外,请注意,您无法“监视...传出...文本”,并且“监视...传入...文本”需要您使用 Android 的未记录功能。虽然可能 android.provider.Telephony 不会消失,但您只需要意识到所涉及的风险即可。

My instinct tells me I need a remote service that has a constantly running broadcast receiver

Um, no. I say that, because:

  • There is only one application, and so there is no need for a "remote" service
  • A "service that has a constantly running broadcast receiver" is a really bad idea, as users will attack you with task killers and, even if they don't, Android will terminate your service eventually anyway as being a waste of space

Use BroadcastReceivers registered in your manifest for broadcasts that can occur at any time, as they will get control even if nothing else of your application is running. Use an IntentService for doing any "heavy lifting" that is needed by those BroadcastReceivers, as BroadcastReceiver get control (in onReceive()) on the main application thread and therefore should do as little work as possible. Also, use AlarmManager to signal a BroadcastReceiver (or, possibly, your IntentService) to raise a Notification for your once-daily event.

Also, please be advised that there is no way for you to "monitor...outgoing...texts", and that to "monitor...incoming...texts" requires you to use an undocumented capability of Android. While probably android.provider.Telephony will not be going away, you just need to be aware of the risk involved.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文