I have an application that has multiple activities and I've set a MediaPlayer for each of them with the same jingle.
I was asking myself, there's a way to keep the music in the first MediaPlayer of the first Activity going on for all the time while the application is running even if it switches activities?
Use foreground service with notification, it runs in background which we can play media player. Here is the example
ForeGround Service start and stop
Related
I have an Android application written in Java that uses an Overlay Service to display a search bubble similar to Facebook chat-heads or One notes Overlay.
The problem I'm facing is that on my new phone (Samsung) (Android 9) overlays get terminated after around 5 minutes of the lock screen. The OneNote Overlay visibly restarts.
I tried to implement the same by checking if was terminated by the system, not the user.
I also found a Broadcast Receiver that calls a function when the phone is unlocked even when my application is in the background.
Here I get the following error:
Not allowed to start service Intent { cmp=com.[...]/.[...] }: app is in background
What can I do to go around that?
Do I have to "push" my app to the foreground, start the overlay, and move it back in the background? is that even possible?
I guess you are using Android 10. (Not sure though). In Android 10 there's a new restriction that prevents you from starting an activity from a background service. More information here: https://developer.android.com/guide/components/activities/background-starts
In an android App im showing advertisement from a local advertisement company.
My problem is when start App and close it with back button, and back to it by clicking on its icon, i see the same Ad. But when i close the app from android task manager and back to the app, i see another Ad.
I know this can have multiple reasons and my question is not clear, But my main questions that how i can set each activity runs like first time of running the app? What should cause this reaction? its like a cookie system... i want every activity show new Ad!
Here you can see example code:
http://github.com/adad-project/client-app-sample
Move
Adad.initialize(getApplicationContext());
((AdView) findViewById(R.id.banner_ad_view)).setAdListener(mAdListener);
to onStart and remove from onCreate in MainActivity
Do similar thing in other activities.
In detail, I want to know if it's possible to take the output from a running Android app and pipe it to the to the background of the display. In other words, have the background wallpaper update itself dynamically.
No you can't do it this way. The reason is because Android starts your app with an activity (unless your app is just made up of services to run in the background for other apps to call). Whenever your activity starts, it comes to the foreground and receives user focus. The Android documentation states that if the user performs an action that starts another activity or switches to a different app, the system calls another set of lifecycle methods on your activity as it moves to the background. At this point, the activity is no longer visible.
The closest concept to what you are trying to do is a live wallpaper but that is a service and is a different paradigm.
I'm developing an Android music player and so my app should be able to detect volume key presses even when no Activity of my app is open and the screen is off(a background service is playing the media). I was thinking that it seems crystal clear that i should detect volume key presses from my service but as I searched the net, I got that it is impossible(e.g. as described here). so what should I do?!!!
You'll need to create a service, because activities don't run when screen is off
You need to run player in service. Also, you must set media type as music.
I am working in an online radio streaming project, where I am playing audio with mediaplayer from an URL. The music is playing and working fine. But I am stuck with the play,pause functionality regarding to the state of the player activity. I want such that when the app will be exited by home button press then music will keep playing, but when a phone call rings then the music will be paused and after the phon call the music will resume. And also when user opens another app then the music will continue playing in background. (as the default music player of android works)
But still what I did results almost my desired destiny, but something is missing, like I have started the music in onResume and stopped the music in onDestroy.By doing this the music keeps playing in background when I press home button, but when a phone call comes and recieved it continues playing which creates a disgusting situation, but when another app is turned on then the music goes off. How should I manage the MediaPlayer that will work like what I want? Any suggession?
Note: I am not using any service here, just an AsyncTask class to play the music.
The topic of MediaPlayer as background service is covered in multiple sources such as this one.
http://www.codeproject.com/Articles/258176/Adding-Background-Music-to-Android-App
You can also find this in the Android official documentation here
http://developer.android.com/guide/topics/media/mediaplayer.html
Hope that helps.