Intercept Android system keyboard shortcuts to override functionality - android

Is it possible for an Android application to intercept external keyboard shortcuts (e.g. Alt+Tab) and action them before the OS performs the default behaviour? Using Alt+Tab as an example, I would like to be able to have my app respond to Alt+Tab within my application and not have Android switch applications to a different app.
I have tried searching, but have not been able to find an answer, I think my Google-Fu is failing me!

It maybe is not as extense or detailed as it would be needed for capturing a shortcut, but you may want to override the onKeyDown event in your activity.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Log.d(this.getClass().getName(), "Back button pressed");
}
if ((keyCode == KeyEvent.KEYCODE_HOME)) {
Log.d(this.getClass().getName(), "Home button pressed");
}
if ((keyCode == KeyEvent.KEYCODE_MENU)) {
Log.d(this.getClass().getName(), "Menu button pressed");
}
//return true;
return super.onKeyDown(keyCode, event);
}

Related

Android: Key Event from Android Box remote controller

I was interested to know how can i catch key/button events from Android TV Box remote controller?
For example, i want a popup menu to show when i click the OK button from remote controller. And i want to catch the next/back key events from remote controller.
Should i use the Key Event class from Android, if yes how should i implement it?
I came across this function but i cannot really make sense of it.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_A:
{
//your Action code
return true;
}
}
return super.onKeyDown(keyCode, event);
}
Thanks in advance.
You should catch key event on dispatchKeyEvent
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
Log.e(TAG, "Key down, code " + event.getKeyCode());
} else if (event.getAction() == KeyEvent.ACTION_UP) {
Log.e(TAG, "Key up, code " + event.getKeyCode());
}
return true;
}
Edit:
First, you should know key map of your remote (it is not the same for all kind of Android TV box), the code above will help you know code of key that you press on the remote. For example, i got key code 3 when i press button BACK on remote.
Then, i want when back key pressed, a Toast message will be show:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
// You should make a constant instead of hard code number 3.
if (event.getAction() == KeyEvent.ACTION_UP && event.getKeyCode == 3) {
Toast.makeText(this, "Hello, you just press BACK", Toast.LENG_LONG).show();
}
return true;
}

In Android Disable ONLY HomeKey Button

Hi I am looking for disabling the Only HomeKey In Android. WHat i am trying to do is
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
return true;
}
if (event.getKeyCode() == KeyEvent.KEYCODE_MENU) {
//action
}
if (event.getKeyCode() == KeyEvent.KEYCODE_HOME) {
//action
}
if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_CENTER) {
//action
}
return false;
}
But by this my back button is too getting disabled. Is there any way to just disable the HomeKey in android. Please reply.
The documentation states:
Home key. This key is handled by the framework and is never delivered
to applications.
More specifically, the key is consumed by the PhoneWindowManager in the method interceptKeyBeforeDispatching(..). In this method the key is handled and consumed, meaning that Android does not allow overriding this functionality.
UPDATE:
The reason why your back-behavior does not work anymore is because you yourself have handled it. The key enters the onKeyDown-method and you can consume the key (yes or no) by returning true or false. By implementing this:
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
return true;
}
You explicitly state that you have handled the Back key in your Activity which also means that the default behavior of "going-back" is overridden. To restore to its original behavior use return super.onKeyDown(keyCode, event);
Yes, it is possible. In you manifest, under your activity deceleration, replace
<category android:name="android.intent.category.LAUNCHER" />
with
<category android:name="android.intent.category.HOME" />
Note that with this change, your application will not be visible in the "All Apps" section. Also, user needs to set you application as the default home screen. Try out "Toddler Lock" application, it handles this scenario nicely.

When I made call through SIP phone, the mobile number edit into my dialed call log that I want to clear, how can I do that?

I have done the following changes into my code. I just want to clear the call log that written by the SIP phone, I have made my SIP phone using SIPDROID.
If I delete the permissions from Android manifest it gives me an error.
All I want is to remove the dialed call logs into the recent calling history.
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK)
{
Log.e("Test", "Back button pressed!");
}
else if(keyCode == KeyEvent.KEYCODE_HOME)
{
Log.e("Test", "Home button pressed!");
onBackPressed();
}
return super.onKeyDown(keyCode, event);
}

Android - handling the back button

I have an app which requires the user to be logged in. In the app, to be logged in is to contain a specific string in the SharedPreferences file.
It is totally working except for one use case: when the user presses the phone's back button.
Is there something different that happens in loading the page when the user presses the phone's back button that I should be aware of and account for in the code?
Thanks!!
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == KeyEvent.KEYCODE_BACK)
{
//do your stuff here
return true;
}
else
return super.onKeyUp(keyCode, event);
}
try the above code to handle back key event

onClick HOME key display alert box in android

I am sincerely trying to display alert box onClick of Home key, but I am not succeeded. So please help me to write the code otherwise, show me the code.
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK )
{
finish();
return false;
}
else if (keyCode == KeyEvent.KEYCODE_HOME)
{
finish();
return false;
}
return super.onKeyDown(keyCode, event);
}
it is not possible to intercept KEYCODE_HOME by external applications:
http://code.google.com/p/android/issues/detail?id=1979
I don't think you can intercept HOME key presses. It is reserved to ensure that you don't get locked within any application.
KEYCODE_HOME : This key is handled by the frame work and is never delivered to applications.
So you can't use it

Categories

Resources