Is it possible to create a foreground service with with a constructor with parameters? #15045
Unanswered
ComptonAlvaro
asked this question in
Q&A
Replies: 1 comment
-
Hello, I recently had the same issue. I think that the request for a default constructor without parameters is required by Android (it seems that foreground service in kotlin/java requires the same). To solve this problem, I simply used a static dependency resolution. Here are some examples: You can call this |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Description
I have a foreground service that it needs another class to do their work. This aditional class implemets the logic that I need, executing a PeriodiTimer() to do some work every 5 minutes.
I would like to use dependency injection for that, so I am trying this:
Really the constructor that I need is the constructor that receive a parameter. However, if I don't implement the default constructor, when I try to compile I get an error that tells that it is needed to have a public default constructor.
If I use the default constructor, I have to create the instance of the service that I need manually, in this way:
And how the service needs a helper, I can't use dependency injection to instantiate the service. Al the classes neededs by the foreground service, can't use dependency injection.
So, is it possible to use a constructor with parameter to create the service?
Perhaps one solution it would be to use foreground service wrapper, that receives can use dependency injection. This wraper would create the foreground service and once it is created, set the service. But I would like to avoid to use a wrapper only to instantite the service, if it would be possible.
Thanks.
Public API Changes
It is not needed to change the API, it is a question.
Intended Use-Case
What I would liek t
Beta Was this translation helpful? Give feedback.
All reactions