问题
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.
From Android documentation:
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.
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.
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+?
回答1:
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.
来源:https://stackoverflow.com/questions/51043954/running-a-service-on-outgoing-calls-in-oreo