0

I was adding sound settings as prefences in my application and I managed to disable and enable the sounds using

   Settings.System.putInt(getContentResolver(), Settings.System.SOUND_EFFECTS_ENABLED, 0); 

and

   Settings.System.putInt(getContentResolver(), Settings.System.SOUND_EFFECTS_ENABLED, 1);

but when the appplication is paused by pressing the home button I need to retain the sounds to the default. I couldn't find any way to recogonize the method of doing it.

I know that trying to do it in any of the activity lifecycle methods is not going to work. is there any way to identify , is my application is in background or running? ( I am thinking about the whole application perspective, so that I can just enable the sound when user minimize it and re-disable when he comes to my application)

thanks in advance

P-RAD
  • 1,293
  • 2
  • 15
  • 36
  • Possible duplicate of [How to detect when an Android app goes to the background and come back to the foreground](http://stackoverflow.com/questions/4414171/how-to-detect-when-an-android-app-goes-to-the-background-and-come-back-to-the-fo) – ant Apr 10 '17 at 09:08

1 Answers1

2

First, I've used an android.app.Application instance (let's call it MyApplication) which has a Timer, a TimerTask, a constant to represent the maximum number of milliseconds that the transition from one activity to another could reasonably take (I went with a value of 2s), and a boolean to indicate whether or not the app was "in the background":

public class MyApplication extends Application {

private Timer mActivityTransitionTimer;
private TimerTask mActivityTransitionTimerTask;
public boolean wasInBackground;
private final long MAX_ACTIVITY_TRANSITION_TIME_MS = 2000;
...

The application also provides two methods for starting and stopping the timer/task:

public void startActivityTransitionTimer() {
this.mActivityTransitionTimer = new Timer();
this.mActivityTransitionTimerTask = new TimerTask() {
    public void run() {
        MyApplication.this.wasInBackground = true;
    }
};

this.mActivityTransitionTimer.schedule(mActivityTransitionTimerTask,
                                       MAX_ACTIVITY_TRANSITION_TIME_MS);
}

public void stopActivityTransitionTimer() {
if (this.mActivityTransitionTimerTask != null) {
    this.mActivityTransitionTimerTask.cancel();
}

if (this.mActivityTransitionTimer != null) {
    this.mActivityTransitionTimer.cancel();
}

this.wasInBackground = false;
}

The last piece of this solution is to add a call to each of these methods from the onResume() and onPause() events of all activities or, preferably, in a base Activity from which all of your concrete Activities inherit:

@Override
public void onResume()
{
super.onResume();

MyApplication myApp = (MyApplication)this.getApplication();
if (myApp.wasInBackground)
{
    //Do specific came-here-from-background code
}

myApp.stopActivityTransitionTimer();
}

@Override
public void onPause()
{
super.onPause();
((MyApplication)this.getApplication()).startActivityTransitionTimer();
}

So in the case when the user is simply navigating between the activities of your app, the onPause() of the departing activity starts the timer, but almost immediately the new activity being entered cancels the timer before it can reach the max transition time. And so wasInBackground would be false.

On the other hand when an Activity comes to the foreground from the Launcher, device wake up, end phone call, etc., more than likely the timer task executed prior to this event, and thus wasInBackground was set to true.

copied from How to detect... Thanks to d60402

Community
  • 1
  • 1
P-RAD
  • 1,293
  • 2
  • 15
  • 36