Android Back Key Issue - java

I'm developing a game via Andengine for Android. I have big problem. When I open my game first time, it's opening well. And when I press back key button(which is on device, pyhsical button), the app is closing. But it mustn't be close. And here is the LogCat:
http://s27.postimg.org/3mhwb3j2b/Capture.png
And when I try to open app again, it is opening black blank scene.
PLS help!

try to override onKeyDown and block BACK button
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode==KeyEvent.KEYCODE_BACK)
return true;
return super.onKeyDown(keyCode, event);
}
That should prevent user from exit App with Back button

You should override the onBackPressed function, and do what ever implementation you like. Usually a pause of the game is common with a dialog to quit or continue is used.
#Override
public void onBackPressed()
{}

Related

Toggle only works the first time

I am trying to use the Toggle Button to play a sound when the button is "on" and stop the sound when the button is "off".
This seems to only work when I first toggle between the on/off button.
ToggleButton toggle = (ToggleButton) findViewById(R.id.toggleButton1);
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// The toggle is enabled
sound1.start();
} else {
// The toggle is disabled
sound1.stop();
}
}
});
Because you called method stop().
There is the description of the method:
Calling stop() stops playback and causes a MediaPlayer in the Started, Paused, Prepared or PlaybackCompleted state to enter the Stopped state.
Once in the Stopped state, playback cannot be started until prepare() or prepareAsync() are called to set the MediaPlayer object to the Prepared state again.
Calling stop() has no effect on a MediaPlayer object that is already in the Stopped state.
How to play exactly right,you just need to visit this website of google. click me.
So the best thing that you can is to use a class constant to check if the flag is already set. You will need to save this to the savedCacheInstance when onPause/onStop/OnDestroy and then reload it. Something like:
boolean isMusicPlaying = false;
If you save this using savedInstanceCache in both onCreate & onStop/onDestroy, you can control the playback between rotations and start ups/exits. The class property is also inside the class scope of the listener so that any action you take, should update the class property. The best way to identify any issue is to of course, use the debugger. That way you can control the play functions and see which is breaking.

Android Application for Kids

I'm Developing Android Application for Parental Controlling, so I want to disable home and back button. Because kids are unpredictable, their can push home button or back button and so on. Than i want to disable sliding notification bar. (Because notification bar can change device configuration and setting)
I'm planning to create Android Application which is similar to the Samsung Kids Mode-Parental Control.
Is possible to create application like Samsung Kids Mode? Can you give some link/article or even example program to me?
I Have tried this :
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
but it not handle home button, the Home button still working.
Thanks in advance to everyone for taking time to read this, hoping a positive solution.
I dont know of anyway to override the home button. Im not sure that it can be done. You can override the back button like so.
#Override
public void onBackPressed() {
//do nothing, important dont call super.onBackPressed
}
Another this you can do is set the app to immersive mode to reduce the chance of accidently exiting the app
https://developer.android.com/training/system-ui/immersive.html
Extended from Activity
#Override
public void onBackPressed() {
}
can be used to override back button. Be sure to remove super.onBackPressed().

Android Global KeyListener necessary?

I want my app to start, after the back button was pressed 3Times.
So I created this class which is executed in the Launcher.
public class KeyManager extends Activity {
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(KeyEvent == "Back key"){
Log.e("KM", "PRESSED!!!!!"+keyCode);
}
//return super.onKeyDown(keyCode, event);
return false;
}
}
This code works, if it s implemented in the launcher class, but it does not work, if the app is in the background. How or which key listener do I need for listening to background keys or more if the app is working only in the background?
This is not possible due security reasons. It's more likely a keylogger. If you really want to do that use an AccessibilityService which let you capture the KeyHooks even on a non-rooted Device. Warning: This is Dangerous and user must explizit enable it, to get it activated.
You may/can also modify the default keyboard, to capture those.
I want my app to start, after the back button was pressed 3Times.
You are welcome to download the Android source code, alter the operating system to contain this feature, compile it into a ROM mod, and install that ROM mod on your device.
Otherwise, what you want is not supported by Android.

How to not save to SharedPreferences if cancel was clicked on a ListPreference

I need to make it so that if cancel or back was pushed when a ListPreference was open that, the value is not saved in SharedPreferences and takes no effect on the app. I have everything in my application working and have been searching for some type of solution but could not find it.
Thanks!
Edit:
Is there a way to start the list preference with none of the values selected?
Alright I was playing around and figured it out. The way I did it was I made a string in the res/values/string.xml and gave it its own id(#+id/none). Then in the res/xml/preferences.xml in the ListPreference info I added android:defaultValue="id/none". Now when I open the list of preferences nothing is selected as default.
You can handle cancel and back press event explicitly in your app and do the stuff accordingly.
Back button can be handled in following way:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// your code
return true;
}
return super.onKeyDown(keyCode, event);
}

set application to run in foreground

Is there any way to set my application to run in foreground while pressing any key, even the home button? I have tried with avtivitymanager, but I didn't get o/p. I think you may be able to help me.
Ashwin Kanjariya
you can stop functioning of all buttons,,,,
You just override the home and back button of device
public boolean onKeyDown(int keyCode, KeyEvent keyEvent) {
if (keyCode == KeyEvent.KEYCODE_Home) {
//finish();
}
return super.onKeyDown(keyCode, keyEvent);
}
In your activity class your application will not be interrupted as the buttton default functionality is overrided as you desire...
I find out that you can catch keypresses by overriding one of the onKey..(int keyCode, KeyEvent event) functions in the activity, but you can not override the home key for some reasons.
home key in adroid API
onkey events
question about button overriding
question about home overriding

Categories