Android - start application from HW button
-
14-11-2019 - |
Question
I need to bind some phone HW button to start my application.
It should be done from the code of the application or when installing it.
Is it possible in Android?
Solution
You can't bind an application to a key like creating shortcut keys in desktop applications.
If your app is not running then only way it can be invoked other than manual press on launcher icon, is by Broadcast Receivers only. But no key press is broadcasted in Android.
One way to achieve this by running a Service in foreground which watches for Key Presses and can initiate your app when specific Key combination is pressed. But not recommended because user may not like this.
OTHER TIPS
Declare this in your manifest.xml file
<activity android:name=".youractivity">
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.MONKEY" />
</intent-filter >
</activity>
No, you can't do that as far as I know and more importantly, even if you could, you shouldn't - it would be terribly UX. The user expects the hardware buttons to perform a certain function, overriding that sounds like a very bad idea.
Imagine if any app you would install could just change what your home button does...