Search code examples
androidbroadcastreceiverandroid-service

Detect button long press on Android


I would like to start a method from my Service with pressing a physical button (home, back, menu ...) If the user long presses one of those buttons, the service should call a method. How can I implement a broadcast receiver or a listener for my case?


Solution

  • A service can't access the UI thread. One thing that you could do is have the activity register the onlongclick listener and then broadcast an intent to the service using a broadcast receiver or the onStart(Intent intent) of the service.