How can i disable all bottom navigation button for lockscreen..? - android

I'm creating lock screen application. and all activity work perfectly, But when screen lock activity comes up on screen on, all Home and task navigation button works. Only back button is not working because I have return nothing in onBackPressed() method.
Now I don't know how to disable this navigation buttons to work in my lockscreen application.
I have tried this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)||(keyCode == KeyEvent.KEYCODE_POWER)||(keyCode == KeyEvent.KEYCODE_VOLUME_UP)||(keyCode == KeyEvent.KEYCODE_CAMERA)) {
//this is where I can do my stuff
return false;
}
if (keyCode == KeyEvent.KEYCODE_MENU) {
//Do Code Here
// If want to block just return false
return false;
}
if (keyCode == KeyEvent.KEYCODE_HOME) {
//Do Code Here
// If want to block just return false
return false;
}
But Only back button stops working all other remain as it is.

Related

Handle keyboard navigation arrows

On a Samsung Tablet, we have the following keyboard:
When a clic occurred on the right bottom arrows, in the view pager, fragment is changed. I want to intercept this event or remove the arrows from the keyboard. Is it possible ?
I tried to intercept keyevent with onKeyUp and onKeyDown methods, but it's not working:
if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT || event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT) {
return true;
}
My problem is the same as here
Try to use dispatchKeyEvent(KeyEvent event):
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
Log.i("key pressed ", String.valueOf(event.getKeyCode()));
if(event.getKeyCode() == KeyEvent.KEY_A /*select required keycode*/ ){
// perform task
}
return super.dispatchKeyEvent(event);
}

Which key action does the nexus phone fire when back button pressed?

I'm wondering which KeyEvent action is called when a user presses the little upside-down triangle in nexus phone when soft keyboard is open.
In normal mode Nexus looks like this and the normal code works fine:
Nexus without keyboard
But when keyboard pops up it looks like this and the code won't work:
Nexus with keyboard
For android API up to 5:
#Override
public void onBackPressed() {
// your code.
}
For android before API 5 you must use this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// your code
return true;
}
return super.onKeyDown(keyCode, event);
}
Please refer to How to handle back button in activity
EDIT:
This methods works only if the keyboard is hidden..
according with this answer: Detect back key press - When keyboard is open
The best action to be implement is dispatchKeyEventPreIme.
An example is:
#Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
Log.d(TAG, "dispatchKeyEventPreIme(" + event + ")");
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
KeyEvent.DispatcherState state = getKeyDispatcherState();
if (state != null) {
if (event.getAction() == KeyEvent.ACTION_DOWN
&& event.getRepeatCount() == 0) {
state.startTracking(event, this);
return true;
} else if (event.getAction() == KeyEvent.ACTION_UP
&& !event.isCanceled() && state.isTracking(event)) {
mActivity.onBackPressed();
return true;
}
}
}
return super.dispatchKeyEventPreIme(event);
}
Where mActivity is your activity class (this).

Disable Back button in Digits

While doing digits programming, i am using following method :
Digits.authenticate(authCallback,R.style.CustomDigitsTheme1);
which directs me to digits authentication screen(without showing my xml design file).
Now, when i press back button, it shows me my xml with digits auth button as below.
which i do not want.I tried conventional ways of disabling back buttons but they did not work. Is there any way i can disable back button on authentication???
You can override when the keyboard disappears using this method:
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK &&
event.getAction() == KeyEvent.ACTION_UP) {
// Do your thing here
return false;
}
return super.dispatchKeyEvent(event);
}
This may helps you.
Try Using these
#Override
public void onBackPressed() {
// your code.
}
and for older then API 5 use this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// your code
return true;
}
return super.onKeyDown(keyCode, event);
}

close android app with down botton

I have a problem with the back button event and a code to close my app. I explain:
I have an app with two activities. The first activity displays a list of events in a listview. When you click on one of these events a new activity that can confirm attendance at the selected event opens. If this second activity you press the button that confirms attendance returns to the main activity.
My problem is that if once you hold an event from the main activity and confirm second activity giving the button that returns you to the main activity when the main activity to give back button instead of closing the app returns to the second activity.
Therefore I want to record the event of pressing the button back and close the app, I tested with:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
android.os.Process.killProcess(android.os.Process.myPid());
return true;
}
return super.onKeyDown(keyCode, event);
}
and too:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
exit(0);
return true;
}
return super.onKeyDown(keyCode, event);
}
thank you very much for your time and help
ANSWER
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory( Intent.CATEGORY_HOME );
homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
return true;
}
return super.onKeyDown(keyCode, event);
}
I think you have set up the activities wrong. You should set a result in the second activity, and then call it's finish() method. Then the back button will always work as expected and you won't have to do weird hacks like that.

android : how could I exit the app?

In my android application I changed the back button functionality so that it goes to the main screen of my game , now that it's on the main screen how should I exit the whole application with back button ?
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK ) {
Assets.getInstance().getClick().play(1);
this.clearScreenStack();
this.setScreen(new MainMenuScreen(this));
return true;
}
return super.onKeyDown(keyCode, event);
}
If you have a mechanism that you can use to see which screen is showing you could do something like this:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK ) {
if(mainScreenIsShowing == true){
//If the main screen is showing let the back button
//have its default behavior.
return super.onKeyDown(keyCode, event);
}else{
Assets.getInstance().getClick().play(1);
this.clearScreenStack();
this.setScreen(new MainMenuScreen(this));
return true;
}
}
return super.onKeyDown(keyCode, event);
}
You can also check if the user enters the back button two times.
boolean backPressed = false;
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && !backPressed) {
Assets.getInstance().getClick().play(1);
this.clearScreenStack();
this.setScreen(new MainMenuScreen(this));
backPressed = true;
return true;
}
backPressed = false;
return super.onKeyDown(keyCode, event);
}
This is a debateable subject, but I see nothing wrong or with an application exiting when the back button is pressed. After all, a call to finish() is the default behaviour of the back button. If the activity handling your main screen is at the bottom of your activity stack, then a call to finish() will exit your application.
I propose the following: Let your MainMenuScreen be handled in a separate activity, MainMenuActivity, which is the main activity. finish() other activities when going back to the MainMenuActivity, and handle onKeyDown like this in MainMenuActivity:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK ) {
this.finish()
}
}

Categories

Resources