-1

I have an Activity and a service running in background. I want to know when user click the Back Key(when my activity is running) in my service so that I can do something in my service. Except override the onKeyDown, do I have an another way? Any ideas?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Paul King
  • 11
  • 3

1 Answers1

4

You can override the onBackPressed() method and in that method call the service / send a message to it.

Mjoellnir
  • 486
  • 5
  • 11
  • I know I can make it in this way. I want to know is there an another way to make it? More clearly. I don't want to add more code in my acitvity, except for that code startservcie(). – Paul King Mar 18 '15 at 06:24
  • More clearly: no. How should that work? The service is a completely independent entity. It knows nothing of the activity, and for good reason. You _have_ to add code. That's how things work. You cannot expect a service to kind of mind read your activity's lifecycle... – Mjoellnir Mar 18 '15 at 07:26