Is there anyway to check if the application is minimized or if you have locked your device? Because when I do minimize / lock my device the application is still runnig, this is mainly because I'd like to pause the music / sfx not to annoy people.. Like if someone is calling.

I am using Activity and SurfaceView with threads.

I have tried putting my pause method in the surfaceDestroyed / surfaceChanged but without success.

有帮助吗?

解决方案

You should understand the activity lifecycle first

enter image description here

when the activity comes int the foreground it'll enter onPause() and it'll enter onResume if the user returns to the activity, an example to use onPause is like this

@Override
public void onPause() {
    super.onPause();  // Always call the superclass method first

    // DO YOUR STUFF HERE
    }
}

for furthere reference about onPause you can see it here -> onPause Tutorial

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top