这种情况下如何做服务呢?

发布于 2024-11-04 16:59:52 字数 212 浏览 5 评论 0原文

所以我有一个类可以监听来电,我想为我的所有应用程序提供一项服务来接收呼叫,即使我不在应用程序的用户界面中也是如此。 如何触发这个类(广播接收者)。我使用“sendBroadcast”并且有一个FC。

sendBroadcast(new Intent(context, IncomingCallReceiver.class));

感谢您的帮助。

So i have a class that listens to incoming calls, i want to make a service to all my app to receive calls even when i'm not in the application's UI.
How to trigger this class(broadcast receiver). I used "sendBroadcast" and have a FC.

sendBroadcast(new Intent(context, IncomingCallReceiver.class));

Thank you for your help.

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

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

发布评论

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

评论(1

很酷又爱笑 2024-11-11 16:59:52

在您的情况下,我将使用以下方法:

  • 创建 Service 并从您的 Activity 启动它(您提到您有多个应用程序,因此启动其中第一个应用程序也可能会启动服务)。
  • 确保服务不会永远工作,因此当您不再需要它时停止该服务(最后一个应用程序已完成)。即使没有 Activity,服务也可以通过调用 stopSelf() 来终止自身。请注意,系统也可能终止您的服务并阻止其永远运行。
  • 在 Service 中创建扩展 BroadcastReceiver 的私有类,并使用 Service 函数 registerReceiver() 为您想要监控的 Intent 注册它。
  • 一旦收到想要的 Intent,您就可以从 BroadcastReceiver onReceive() 中调用一些服务函数。例如,您可以使用您的应用程序识别的一些自定义 Intent 来 sendBroadcast() 。
  • 当服务停止时,请确保使用服务函数 unregisterReceiver() 取消注册 BroadcastReceiver 扩展。

更新:

In your case I would use the following approach:

  • Create Service and start it from your Activity (you mentioned that you have several applications, so starting first of them may also start the Service).
  • Make sure that Service does not work forever, so stop the service when you do not need it any more (last of your applications is finished). Service may terminate self even without Activity by calling stopSelf(). Please note that also System may terminate your Service and prevent it from working forever.
  • Make private class within Service that extends BroadcastReceiver and register it for the Intents you want to monitor using Service function registerReceiver().
  • Once you receive wanted Intent, you can call some Service function from within BroadcastReceiver onReceive(). For example, you may sendBroadcast() using some custom Intent that is recognized by your applications.
  • When Service is stopped make sure that you un-register the BroadcastReceiver extension using Service function unregisterReceiver().

UPDATE:

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