Toggle only works the first time - java

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.

Related

Start activity in onstop() method without showing it on the screen in the background until user re-open app

I want to start an activity in the onStop() method without the activity being loaded popping up on the screen which will disturb the user, I want it to just load the activity with the onstop() method in the background so that when user return to the app later on, user could see the activity being loaded, is there any way to make this possible?
I have read so many of the answers about this matter and tried LifeCycleObserver but the intent still pop up, I want it to stay in the background until user open the app again. Thank you in advance
in your_Application class create variable:
Boolean stateOn = false;
in your onStop() set value true,
like
MyApplication.stateOn = true;
in onDestroy() make it false again, (before calling super.onDestroy())
MyApplication.stateOn = false;
and in on onResume() check it, if it was true , start another activity
if (MyApplication.stateOn) {
startActivity({intent});
}
you can crate your_AppCompatActivity class and do above code in there, to appling this config in all activities who extends your_AppCompatActivity

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 Back Key Issue

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()
{}

How to stop mediaplayer when home button is pressed

I've tried overriding onPause and everything but the mp3 file continues to play even after the home button is pressed.
I have logged the onPause method and it does seem to be working, however it is not stopping the media player.
I believe that when the home button is pressed the object in which my mediaplayer is held in is set to NULL. Is there anyway to stop the music when the home button is pressed?
User870380 is correct, the home key cannot be overridden, and for good reason. The MediaPlayer is quite erratic and buggy, so seeing your exact code will be very useful. Your code will probably look something like this:
#Override
protected void onPause() {
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
}
You may also need to check to see if it throws an IllegalStateException. The MediaPlayer does that. A lot.
Are you getting any errors in your logcat when you try to stop the MediaPlayer?
Are you running the MediaPlayer in a service?

How to know if a button is being pressed or released?

I have a little problem when I try to run my Android application. The problem occurs when I push & release a button on my main View. The application is to behave like a portophone (push the button to talk, release to stop).
To notify that the button is pressed or released, I've tried 2 options:
- Adding an onClickListener,
- Adding an onTouchListener.
The first only looks for the button being pressed, so it's not a viable solution. The second solution is able to differentiate between pushed & released, so it's the one I'm using now. The code looks like this:
// Initialise handler for Push To Talk button
Button button = (Button) findViewById(R.id.buttonPTT);
button.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent me){
switch(me.getAction()){
case MotionEvent.ACTION_DOWN:
Toast.makeText(getApplicationContext(), "PTT pressed", Toast.LENGTH_LONG).show();
// TODO: Activate Push To Talk
break;
case MotionEvent.ACTION_UP:
Toast.makeText(getApplicationContext(), "PTT released", Toast.LENGTH_LONG).show();
// TODO: End Push To Talk
break;
}
return false;
}
});
The problem lies in the ACTION_UP case of the switch. While running, it will only work when you press the button, move the cursor/finger/etc, and then release the button. What I want it to do is press it, and being able to release it without having to move my cursor/finger/etc.
Anybody have any ideas?
Nevermind, I already seem to have found the error. It seemed I still had an onClick handler set in my XML layout file, which I forgot to remove.
Thanks anyway for your time if you read this.

Categories