开发者

Pause in android

开发者 https://www.devze.com 2023-04-12 22:47 出处:网络
I have developed a game开发者_JS百科 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.

I have developed a game开发者_JS百科 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?

Pause in android


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.


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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消