I have seen this post here (http://stackoverflow.com/questions/151777/how-do-i-save-an-android-applications-state). I am having a similar problem I believe.
I have an app which listens to an audio stream (using the mediaPlayer object). If I press the Home button it will continue streaming and hide my app. Then, at a later point I can go back to my app and press stop when I'm done. This is what I want. If however I press the Back button, when I later open my app again the app has been redrawn from fresh. Text boxes, buttons, everything has reset like I've just opened the app for the first time so I can't stop my audio stream. Clicking stop does nothing because the app has 'forgotten' it is streaming (the stream runs under a separate handler from the main UI thread, so I'm guessing since its been 'reset' it has lost track of its handlers?).
Why does this happen with the Back button, and how can I stop it?
Move the streaming functionality into an Android Service. Use an Activity to bind to the Service and to interact with it.
Related
I am working on an App that should perform a function when a certain sequence of buttons are pressed from anywhere in an Android device. For example, if the volume down button is hold pressed for more than 10 seconds and activity in the app will start.
I have been looking up stuff, and all I seem to get is how you can listen to the button press whilst the App is open. Kindly, help on how I can do this with the App closed, or at least in background.
I saw lots of similar questions, but I couldn't figure out the best way in my case.
I want to play some background music in my app (looped). It should start (if a sharedPreference is set) at the start of the app. If the user disable a checkbox, it should stop (if he enabled it, it should start again, preferences is set here...).
Nearly everything worked fine by using an MediaPlayer. But I don't know, how to stop the music if the user tabs the home button. Because of playing music across Activities, I think it doesn't seem wise to stop in onPause() or onDestroy()?!
I read a lot about AsyncTask and Services, because I know it's not good to play the music in the MainThread instead of an WorkerThread. But I couldn't figure out the best solution and how to handle it.
Is there a way to play music until I say explicitly "stop" (in any Activity) or the user tabs the home screen button without editing each Activity?
I'm trying to create a really simple application for Android that will play a music file (I'm really only just starting to get into Android). I have only one Activity that starts when the application starts, and it starts playing the music file. What I need is that the activity always runs (plays the music), whether you press Back or Home buttons, unless you specifically tell it to shut down from Settings menu, and if you try to run it again, it should just restore that activity to the front (basically, how every other player out there works). What happens for me, though, is that when I press back to return to the menu screen for instance, and click on the app again, it runs another instance of the activity (which I can tell, because the music doubles). What can I do to prevent this? Many thanks.
For playing music in the background I would recommend you using a service.
Specify android:launchMode= "singleInstance" in your manifest file. This means that your activity is your entire application.
Don't forget to save the state of your time of the music. Use SharedPreferences for saving an integer with the second when the sound ended playing and just restore the state in onResume() method.
Unfortunately, you cannot play music after you press back button as the activity is destroyed. You must start a service if you wish to do that as the other answer suggests. The reason is that you need a Context object to play music and it will no longer be available after onDestroy() method is called.
http://developer.android.com/guide/topics/media/mediaplayer.html
Here you can find examples of playing media files in a service.
I have written an Android app that plays some audio. There is a stop button on the app GUI that when I first lunch the app works fine.
However when I go out of the app while audio is playing and come back depending on how I came back the STOP button works or not.
If I come back to the app by holding the home button and seeing the list of recent apps and choosing my app from there, then the STOP button works. But if I click on the app luncher icon the STOP button does not work.
What is the difference between these two method and how can I make the re-lunch of the app by pressing on the app icon to behave similar to when I re-lunch the app by choosing the app from the list of recent lunched apps.
Without seeing the code we can't be entirely sure, but it sounds like what is happening is that your activity is you set up an action listener (setOnClickListener) on your stop button in your onCreate() method.
The onCreate() doesn't get called again if the app is never recycled (Android will do this when your app is put into the background) and started over.
When your app is put into the background onPause() will get called, then coming back from that you will get a call to onResume(). If your app has been in the background longer or Android needed more resources you'll get a call to onStop hitting the home button and onStart when the app opens again.
You'll need to do some investigation into your code as to why your listener goes away, but now you have the hooks to make sure they are connected back up when you're app is back.
basically, what am doing is that am running a flash file (playing music) in the background of the app using webview, and when I want to stop the music , I just load "about:blank"
I want to keep the flash file running (music) when my app gets paused (which works fine so far) the problem is when the app resumes, pressing on pause button launches a new webview instead of changing the link in the previous one and the sound doesn't stop
the only solution I found was to kill the process and restart the app, but that's not practical, any idea of how to still be able to use the same webview when resuming the app ?
Update: guess I wasn't clear enough, I have a webview widget stated as "gone" , I only need the audio form the flash file I'm running in the webview so the "play" button loads a URL that contains the flash file (so the user only hears music, and doesn't notice that I'm using a webview), when I press the back/home button the music doesn't stop (I like it that way) but when I go back and press on play again it loads another url in a new page, you get double music
There are a few possible causes for this. One is simply that multiple instances of your activity are being created. Assuming you don't want this, add android:launchMode="singleTask" or android:launchMode="singleInstance" to your main <activity> in AndroidManifest.xml. More info here:
http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
If that doesn't help, another possibility is that your application is creating a new instance of the WebView -- either because the relevant code is in the wrong place (e.g. onStart()), or because the app has actually been destroyed and re-created by Android rather than paused and resumed -- and the old WebView thread is still hanging around for some reason. More information (such as code samples) would be helpful in identifying the problem, if that's the case.
Not sure about this solution but at least you should try this once
protected void onResume() {
super.onResume();
activityTermsAndConditionBinding.webView.releasePointerCapture();
activityTermsAndConditionBinding.webView.invalidate();
}