Now I have a mp3 player that can play music, and I'm wondering should i use service instead of activity.
I have two ways to play music in background (continues to play when user press back button)
When user press back button, I override onBackPressed() and move the activity to background like the way user press home button, so when user launch my app again, it will be displayed as expected.
I read some article about overriding onBackPressed() and some people said that it would not a good way to do some works in background, the better way is using service. :). So my idea is when user press back button, I call finish() to destroy and release the activity, then I create a service that can play music from the point that activity is finished and a notification to help user can go back the main activity.
I think the first approach is very simple and easy to implement but not recommended. And the second one is better but more complex and I don't think this way optimizes memory rather then the first one.
I'm very wondering about which one I should use. Please tell me the better one among two ways above or if you have experience working on something similar lately, please tell your approach in this situation.
Thank you.
You should use a Service for that.
And ideally you should use a Ongoing Notification
This will prevent your service from being killed by the System.
Look at the mediaplayer guide
If I understood your question properly, you should use a service.
Please refer to the media player guide.
Related
I am developing an android app which has multiple activities. I have added a soundtrack also. What I want is to keep playing sound when user is switching between activities but as soon as user leaves it should stop. Help me please
To navigate transitions between stages of the activity lifecycle, the Activity class provides a core set of six callbacks: onCreate(), onStart(), onResume(), onPause(), onStop(), and onDestroy(). The system invokes each of these callbacks as an activity enters a new state. Fore more information click here
Figure 1 presents a visual representation of this paradigm.
To work some thing in the background , you need to use the Service.
for example you may refer :this sample from Google
In the above code Find How MusicService used to achieve your problem. for more search "Android service example".. hope it helps..
MusicService link
I guess I still need to learn how Android apps flow. The title might not have been clear, so let me explain.
Situation:
I have a game which has a few different activities. For example, MenuActivity, GameActivity, and HowToActivity. The game starts at MenuActivity and plays a song set to loop. To have the same song play during MenuActivity and HowToActivity, uniterrupted, I have the song played from an implemented Application. If I press the home button, get a phone, or whatever, the song will continue to play. To prevent that, I need to stop the song when leaving the app.
Problem:
Currently, in MenuActivity, I have code to stop the song under the protected void onStop() function. This stops the song when leaving the app (Pressing the Home button, get a phone call), but it also stops the song when changing to another activity within the app, such as HowToActivity. So the question is, how can I tell the difference?
Jesse,
You need to have a service that will do the job of playing the song.
You can easily start the service from any of the activity of your application. Also the service can be stopped by any of the activity.
Hence in the activity onCreate(), you can start the song player service, that will play the song even if the activity dies and new activity starts. Once your application is done with the song playing, just call stopService().
I hope this will solve your issue.
~Rajan
Typically what happens is that people read the phone state using a PhoneStateListener:
http://developer.android.com/reference/android/telephony/PhoneStateListener.html
This is why so many apps need the READ_PHONE_STATE permission, they're making sure you aren't answering a call while the app goes off and continues to do something annoying. You can create a listener to check when things like this happen. You shouldn't really change the behavior of the home key (and can't!), but instead, you can always listen for things like onPause() and onStop().
You probably want a background service that actually does the music playing, etc.., and then you want to control this service from your actual app when you get lifecycle events inside activities. This makes your app a bit more modular (i.e., the thing that it's doing semantically is control the sound, download the stream, whatever), because the Activities control the UI, and the Services what happens behind the scenes.
Edit: tutorial for MediaPlayer from a service:
http://marakana.com/forums/android/examples/60.html
You might also want to look into using a wake lock, though it might not be strictly necessary.
Create a receiver to capture the following intent:
Intent.ACTION_CLOSE_SYSTEM_DIALOGS
This will be called when the Home screen of the phone is launched. So you can stop music at that time rather than stopping onStop() of Activity.
But this will not help if the user launch an app by pressing Home key long time.
So try to play different musics on different Screens.
I am making an app and required to execute its main functionality only by clicking on its icon.
In other words no layout no widget just its working.
As soon as user click on the icon it should execute its main functionality. One way I think of is to put all the code in onCreate function.
Or if there is any other way please share with me.
You must have an Activity that the user launches. That activity can just call finish() by the time it returns from onResume() to not be shown (you'll also need to set android:theme="#android:style/Theme.NoDisplay"). The actual work can happen in onCreate() or such of the activity if it is very brief, otherwise you'll need to start a service which takes care of doing the work in its thread.
HOWEVER.
Having an app icon that does nothing when the user touches it is a pretty poor experience. I strongly recommend against that. You shouldn't have a main entry into your app that doesn't actually launch the app. I can assure you, doing so will result in your app rating getting lowered due to people not understanding your app. (Especially if this is actually the main activity if your app, because they will hit the "open" button in market and it will do nothing. Sucktastic.)
I don't know what you are doing, but buttons for the user to press from their home screen should generally be implemented with a widget, like the power widget. This makes it much more clear to the user what is going on, especially since visually you can make this look like a button they are pressing rather than an app they are launching.
If you just want to do some job on the background you could use a Service, this could be invoked on the onCreate method of the activity. Otherwise you could use a thread to perform your required task, and execute this thread again on the onCreate of the activity. Is that what you meant?
I would like to let user to leave my application by hiding it but not finishing it.
I could call finish() in my main activity, but it takes some time to do that. It's not good for user experience, therefore a better choice might be to hide it. Just I don't know how to achieve it.
Thanks for all the answer.
I am not trying to do something in the background, because I already have a service.
In my application, user might press an button to close my main activity. However, it take a little time to do that. At least after 0.5 sec, then I'll see my application disappear on screen.
However, if I press home key. My application is disappear immediately, so it's the effect I need.
You could try to move your activity in the background
moveTaskToBack(true);
If you need your app to continue running in the background, spawn the background code in a Service.
http://developer.android.com/reference/android/app/Service.html
The best advice here is simply to start looking at all of the garbage collection the app has to do and make sure you only keep things (connections, etc) open for as long as absolutely necessary.
Point is to limit the amount of stuff the app has to do to finally shut down.
I'm trying to write a very simple app that will do just one very simple non-GUI action, then display a short message (using toast, on top of what was already on the screen, e.g. home screen), and finish. That's all.
This almost works, but i'm still getting a brief black screen after the app starts, then back to home screen and things work as i intend after that. I thought that not using setContentView on my activity (thus depriving it of a View) would be enough to avoid the black screen, but it still pops up.
Please notice that (as suggested in a related question), a service is not the answer to my problem. I just want a GUI-less (except for one toast) app that runs and ends right away.
Thanks :)
In <activity> in your manifest use
android:theme="#android:style/Theme.NoDisplay"
Note this assumes you will call finish() before returning from onResume(). If you are going to do more work after that (for example if you will be doing any networking or other such thing that can't be done quickly synchronously), you will probably want to start a service to take care of the work to tell the platform your process should continue running after the activity finishes.
Sounds like a job for an IntentService.
You send it an intent, and if it isn't running it starts, then it treats all received intents, then it stops. Short and simple.
a service is not the answer to my
problem
Yes it is