在奥利奥(Oreo)中对拨出电话运行服务 [英] Running a service on Outgoing Calls in Oreo
问题描述
我正在开发一个应用程序,该应用程序在每次调用特定号码时都需要运行一些代码(开放式活动).在API 24及更低版本中,我可以在清单文件中注册一个隐式接收器,并在扩展了BroadcastReceiver的指定类中启动我的服务.但是,在API 26中,您无法在接收器中注册android.provider.Telephony.OUTGOING_CALL,因为它无法正常工作.
I'm developing an app which needs to run some code (Open Activity) whenever an call to specific number is performed. In API 24 and lower it's fine, I register an implicit receiver in Manifest file and start my service in the specified class which extended BroadcastReceiver. In API 26 however you cannot register android.provider.Telephony.OUTGOING_CALL in a receiver since it won't work.
来自Android文档:
From Android documentation:
注意:如果您的应用定位到API级别26或更高级别,则不能使用清单,声明隐式广播的接收方(广播并不专门针对您的应用),除了一些隐式不受该限制的广播.在大多数情况下,您可以改为使用预定的作业.
Note: If your app targets API level 26 or higher, you cannot use the manifest to declare a receiver for implicit broadcasts (broadcasts that do not target your app specifically), except for a few implicit broadcasts that are exempted from that restriction. In most cases, you can use scheduled jobs instead.
我已经阅读了几篇类似的文章 https://medium.com/@benexus/background-services-in-android-o-862121d96c95 .有诸如 JobScheduler
或 Explicit Receiver
之类的解决方案,但是第一个用于网络状态的更改,我找不到在OUTGOING_CALL事件中触发作业的方法,并且在您的活动启动并运行之前,第二个有效.
I've read several articles like this one https://medium.com/@benexus/background-services-in-android-o-862121d96c95 on medium. There are solutions like JobScheduler
or Explicit Receiver
, however the first one is used for changes in network state and I couldn't find a way to trigger the job on OUTGOING_CALL event and the second one is valid until your activity is up and running.
由于我的应用程序的性质,无论该应用程序是否运行,我都需要监听去电.如何在API 26+中做到这一点?
Because of the nature of my application I need to listen for outgoing call whether the app is running or not. How to do that in API 26+?
推荐答案
在此处注意 https://developer.android.com/guide/components/broadcast-exceptions ,ACTION_NEW_OUTGOING_CALL不受您提到的Oreo限制.
Notice here https://developer.android.com/guide/components/broadcast-exceptions that ACTION_NEW_OUTGOING_CALL is exempted from the Oreo restrictions you've mentioned.
如果您尝试了一些代码,但是没有用,那么您应该发布该代码.
If you have code that you've tried but it isn't working, then you should post that.
这篇关于在奥利奥(Oreo)中对拨出电话运行服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!