0

I'm trying to hack hardware key support into an existing app, but came to realize I know too little of Android app development to continue.
I can decompile and recompile the app without problems using APKtool, and I made a small app myself with hardware key support to pull the compiled smali code out of. For reference, that part looks like this:

.method public onKeyDown(ILandroid/view/KeyEvent;)Z

If I want to have the app listen for hardware keys "globally" (i.e. in every activity, window, what-have-you, of the app), where ought I put the onKeyDown code? Do I put it in a single activity (if so, which one), in all activities, or someplace else altogether?

Edit: I realize "globally" is a bit ambiguous here. I mean throughout the entire app, not system-wide.

Protector one
  • 6,926
  • 5
  • 62
  • 86
  • Our very own balpha recently posted some good information on listeners in Android, scroll to Propagation of Events: http://balpha.de/2013/07/android-development-what-i-wish-i-had-known-earlier/ – Dave Swersky Aug 06 '13 at 17:04
  • That's certainly useful information, although I'm not confident enough to assume key events propagate in the same manner as touch events. – Protector one Aug 06 '13 at 17:21

1 Answers1

1

If I want to have the app listen for hardware keys "globally" (i.e. in every activity, window, what-have-you, of the app), where ought I put the onKeyDown code?

You download the Android source code, modify it to have some sort of global hardware key handler, compile the modified source, package it into a ROM mod, and install the ROM mod on your device.

Otherwise, only the foreground activity (or, sometimes, window) will receive key events.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Oh, but that's OK! I want it "globally for the app", and the app in question is one that doesn't run in the background. – Protector one Aug 06 '13 at 17:28
  • @Protectorone: There is no concept of hardware key processing "globally for the app". Hence, either you mod Android, or you register your handlers on a per-activity (or per-window) basis. – CommonsWare Aug 06 '13 at 17:31
  • There is no "master activity" of sorts that can intercept keys throughout the entire app? I really have to put it in every activity and window? – Protector one Aug 06 '13 at 17:33
  • @Protectorone: "There is no "master activity" of sorts that can intercept keys throughout the entire app?" -- correct. "I really have to put it in every activity and window?" -- correct. Now, if you were the actual author of the app, you might use inheritance or helper methods or something to minimize the amount of redundant code. – CommonsWare Aug 06 '13 at 17:45
  • Well that's an interesting point! Who is to say this app's creator hasn't already? If all activities in it are inherited from a master activity/window, would it suffice to put the code there? – Protector one Aug 06 '13 at 19:05
  • @Protectorone: "would it suffice to put the code there?" -- that's impossible to answer in the abstract, sorry. – CommonsWare Aug 06 '13 at 19:14