Reputation: 73
I've been working on a security-related app. This app sends the location data to the server periodically on the background. But I need to open the app (and click on a button) to start the service.
Is there a way to start this service without the need to open the app in the foreground? (Like shaking the device or clicking several times on the volume buttons)
I'm thinking maybe to start a listener on the background (through the UI) that executes the service when activated via the volume buttons?
Any help will be appreciated, thanks!
Upvotes: 0
Views: 854
Reputation: 3298
Because of the updated stranded you can't keep running a service in background. To solve your purpose you can use foreground service instead. Here is the package for implementing foreground service.
https://pub.dev/packages/foreground_service
In that service you can write code for detecting Shake or Volume Button press. You can get help from the links below..
Detect shake event https://stackoverflow.com/a/5271532/6413387
Detect Volume Button event https://stackoverflow.com/a/9162794/6413387
Upvotes: 1