flutter-moum / flutter_hardware_buttons

Flutter plugin for detecting all hardware buttons
MIT License
37 stars 47 forks source link

Use on background service #40

Closed dgalizzi closed 4 years ago

dgalizzi commented 4 years ago

Hello, Is it possible to access hardware buttons on headless? I'm getting null error on activity, found this issue on another repository which I believe is related:

https://github.com/transistorsoft/flutter_background_fetch/issues/11#issuecomment-462892194

giantsol commented 4 years ago

Hi @dgalizzi I see.. so headless plugin runs a background service, and you were trying to access hardware buttons in that service while there was no activity. Is that right?

Unfortunately, it isn't possible to use this plugin on headless due to 2 reasons.

  1. To solve your problem, it means this plugin needs to work without activity instance. But some main mechanisms with this plugin needs activity, for instance volume button detection and requesting permissions.
  2. We designed this plugin to work only when the app is in foreground (i.e. the activity is visible). As soon as the app goes background, we stop detecting hardware buttons. We adopted this policy because hardware button is kind of sensitive, and it looked reasonable that apps shouldn't keep listening to them if not visible.

We may have to guard ourselves for that crash, though.

Thanks!

dgalizzi commented 4 years ago

Hi,

so headless plugin runs a background service, and you were trying to access hardware buttons in that service while there was no activity. Is that right?

That's exactly it. I want to run some code when volume buttons are pressed but the application on the background. Like some music apps allow you to go to the next song on the playlist by pressing the volume buttons while the phone is locked.

Thanks for your answer, I understand the issue.