Android - handling the back button - android

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

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;
}

Intercept Android system keyboard shortcuts to override functionality

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);
}

Trialing an android application - want to disable any way to get out the app - home button etc

I am part of a team developing an android application and we are trialing it for one day soon.
I was wondering if there is a way to "lock" the users into the app, (most users aren't smartphone literate so don't want them hitting the home button by accident and then being confused about how to get back).
I know there is a reason this probably isn't possible (due to malware apps locking users out of their phone essentially) but is there any way to do this?
I've looked around on SO and found some discussions but couldn't find a concrete answer.
We have Nexus 4 phones running Jelly Bean 4.2.2
#Override
public void onAttachedToWindow() {
// TODO Auto-generated method stub
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
and override home key press event in on key down method.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == KeyEvent.KEYCODE_HOME) {
Toast.makeText(this, "Sorry", Toast.LENGTH_SHORT).show();
return true;
} else if (keyCode == KeyEvent.KEYCODE_BACK) {
Toast.makeText(this, "Sorry No back press", Toast.LENGTH_SHORT).show();
return true;
} else {
return super.onKeyDown(keyCode, event);
}
}

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);
}

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