Nikunj Patel
Nikunj Patel

Reputation: 22066

Pause in android

I have developed a game in which i need a pause in any single button click event. I am confused because I have use swf file and sound and much resource.

As shown, when you pause the screen it will goto sleep mode and the resume button is in front of you.

How can i do this?

enter image description here

Upvotes: 2

Views: 884

Answers (4)

aalionline
aalionline

Reputation: 448

Pls First Understand the Hierarchy of the activities from here : MediaPlayer | Android Developers

To pause call the function:        public void pause() 
To Start/Resume call the function: public void start()

Run the media file in the background in a Service. See the detail code here :

Resuming Service in Activity and destroying them on destroy | Stack Overflow

Upvotes: 1

Karl John Hernandez
Karl John Hernandez

Reputation: 328

maybe you can use this in your game Activity, this is how I pause my game in webView with flash

@Override
public void onWindowFocusChanged(boolean hasFocus) {
gameView = (webView) findViewById(R.id.wView);
    if (hasFocus) {
        try {
            Class.forName("android.webkit.WebView")
                    .getMethod("onResume", (Class[]) null)
                    .invoke(gameView, (Object[]) null);
        } catch (Exception e) {

        }
        gameView.resumeTimers();
        gameView.loadUrl("javascript:setflashfocus()");
    } else {
        try {
            Class.forName("android.webkit.WebView")
                    .getMethod("onPause", (Class[]) null)
                    .invoke(gameView, (Object[]) null);
        } catch (Exception e) {

        }
        gameView.pauseTimers();
    }
    super.onWindowFocusChanged(hasFocus);
}

maybe you cn launch another activity for this to execute

Upvotes: 1

Dinesh Prajapati
Dinesh Prajapati

Reputation: 9520

For this If the Activity is running and user has pressed the pause button and that time you can call the function which can pause your resources.

This includes 1. Pausing of Media file which is being played this you can do with the help of MediaPlayer API 2. Animation of the view you can stop 3. Notification sound you can stop with the help of Notification API

Upvotes: 1

Pramod
Pramod

Reputation: 5208

You could save all the required details in the activity's onPause method.

When the button for pausing is pressed, you could start one more activity (maybe a dialog saying the game has been paused).

Resume the game using the onResume method of your activity.

Upvotes: 4

Related Questions