Add background music through out app - android

I'm having trouble adding background music to my app. I have three activities and I would like the music to keep playing regardless of whether the user switches activities or not. I am currently using a service to play the music but this results in the music starting and stopping every time a different activity starts.
I have found this solution several times throughout forums but it doesn't seem work:
Context context = getApplicationContext();
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
if (!taskInfo.isEmpty()) {
ComponentName topActivity = taskInfo.get(0).topActivity;
if (!topActivity.getPackageName().equals(context.getPackageName())) {
player.stop();
}
}
You're supposed to add it to every activities onPause, and I did that and it still didn't work. The music doesn't stop when you switch between activities, but it also doesn't stop when you exit the app.
Thanks in advance for the help.

How is the service setup? Are you binding the service to an activity or are you passing an intent to the service. If the service is bound to an activity, that would explain why when you navigate to a different activity, that the service dies. The activity that the service is bound to is no longer there.
If you are running and passing an intent to the service, then hopefully, you aren't passing anything to the service to stop playback. If you navigate outside the app, the service should still be running in the foreground with a notification on your notification bar.
http://developer.android.com/guide/topics/media/mediaplayer.html
You might want to take a look at the guide I linked above.

Related

Android is killing activity while the service controlled by it lives on

I have an app which has a single activity which has a checkbox the starts and stops a foreground service. The activity also has some other settings which control that service.
The problem is that after some time navigating other apps the service continues to run fine but the activity gets killed. If I restart the activity the checkbox will be set to default, off. If I switch it to on, a second service starts, while the first one is still running and can't be controlled anymore.
I can't figure out how to handle this situation. It's necessary for the service to run indefinitely and there doesn't seem to be anyway to keep the activity from dying. Maybe there's a way for the new activity to find the old service?
Try to kill the service in OnPause() method of your activity .that solve my problem few times ago.
To check if particular service is running, write the below method your activity, and call it when you want to check the state of the service
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
"service continues to run fine but the activity gets killed"
services are given more importance than activities, if android where given a choice to kill an activity or a service it always kill an activity first.
"a second service starts"
services can only be started once no matter how many times you start a service.
"checkbox will be set to default"
use sharedpreferences to persist values.

Android: How to know what application is opened to foreground and do something when it goes to background

The title is bad, sorry. Could figure out how to say this in short.
So, I'm developing an application for Android which will only be used in my own couple tablets which are going to public use.
I need a service running in the background (or so I currently believe, prove me wrong if possible) which keeps track which application is foreground, and when that application goes to background the service starts activity which will go foreground and show something to the user(for example like some ad, or some review window which asks for start rating).
How could this be done?
Thank you.
E: The other applications can be 3rd party, so I can't modify the code of those.
When u start other activity at that time you current activity is OnPause() state and other activity goes onCreate() State.
You Must refer Activity LifeCycle for this.
You have to use Accessibility service. Starting from Android Lollipop you can't gather this kind of information. The only way is via accessibility docs.
to check other running tasks you should use ActivityManager.
ActivityManager acm = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> Ru_tasks = acm.getRunningTasks(NUMBER_OF_TASKS_U_WANT);
if (!Ru_tasks .isEmpty()) {
LOOP FOR EACH TASK (i is counter)
ComponentName topActivity = tasks.get(i).topActivity; // Or replace .topactivity with task name
if (topActivity.getPackageName().equals(WHAT_YOU LOOK FOR)) {
//Do WHAT YOU WANT!
}
}
get GET_TASKS permission

Close the service from the library only when the entire app goes to the background

I searched a lot in the net about it, the solutions are based on onPause and onDestroy. I want to give a library to a developer who just needs to paste a few lines of code from the library in his app which will enable the developer to create a service and destroy it when his entire app is in the background.
Does the Android OS send some kind of signals or intents when the app or the activity is changed (other than onPause or onStop method), so that i can catch that in a broadcast receiver from my library and do some actions.
If you know the package name of your app, try this (put this following snippet in the onCreate method of your app):
ActivityManager am= (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
Then,
boolean exit = false;
while(!exit)
{
List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
if(!componentInfo.getPackageName().equals("Your package name"))
{
//Do your work here
exit = true;
}
}
When you start your app, this will be put into componentInfo. The taskInfo.get(0).topActivity will return the activity in the foreground. Hence you can know that your app has gone to the background by comparing package using the second code snippet.
Note:Put this second code snippet in an Asynctask so that the checking of whether the app has started can be done in the background.

Android: test from a service, if an Activity is running.

I have a service that runs in background. I I wish it could perform an action, only if a certain activity is running. How can I test, from a service, if the activity is running?
You can use the Package Manager and the Activity Manager:
PackageManager: Class for retrieving various kinds of information related to the application packages that are currently installed on the device. You can find this class through getPackageManager().
ActivityManager: Interact with the overall activities running in the system.
But, a more simple approach would be: When starting your Activity (onCreate/onResume), send a message with service.send(..) or an Intent with service.onStartCommand(..) so the service knows that your particular Activity has been started.
Maybe you can do it the other way around, sending intent from your activity to the service.
For example, you can send an intent on the activity onResume, telling your service the activity is live, and another on onPause, telling the service the activity has stopped
Use the below method with your package name.It will return true if any of your activity is in foreground.
public boolean isForeground(String myPackage){
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List< ActivityManager.RunningTaskInfo > runningTaskInfo = am.getRunningTasks(1);
ComponentName componentInfo = runningTaskInfo.get(0).topActivity;
if(componentInfo.getPackageName().equals(myPackage)) return true;
return false;
}

Detect when Android app is going to background

In my application I need to detect whether my application is going to background or is switching to another activity of the same application... I know that I have to use the onPause method... but how can I distinguish the two cases?
private static boolean isApplicationGoingToBackground(final Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).topActivity;
if (!topActivity.getPackageName().equals(context.getPackageName())) {
return true;
}
}
return false;
}
UPDATE: getRunningTasks has been declared to not be guaranteed to be accurate.
Call in onStop. onStop gets called after onStart of whatever takes over the screen - if its an activity in the same apk package then you're not going into the background. This does require the GET_TASKS permission.
Or bind to a service onStart & unbind onStop - the service will then be onDestroyed when all your activities are stopped (or track binds vs unbinds if you don't want to rely on onDestroyed getting called - because it might not..).
There is no direct approach to get the app status while background or foreground, but even i have faced this issue, and found the solution with onWindowFocusChanged and onStop. For more details: http://vardhan-justlikethat.blogspot.in/2013/05/android-solution-to-detect-when-android.html

Categories

Resources