簡體   English   中英

從后台恢復后,andengine / OpenGL應用程序凍結

[英]Andengine/OpenGL app freezes after resuming from background

我開發了一個andengine應用程序,該應用程序運行正常,除非該應用程序進入后台運行。 我看到了2個問題。

1)如果我按下主屏幕按鈕離開應用程序,然后再次返回,它只會顯示黑屏,並且沒有任何反應(無交互)。

2)如果我在應用程序中展示廣告並關閉該應用程序,則該應用程序凍結,並且游戲中沒有任何動作(觸摸,按按鈕等)

我不知道發生了什么事? 這是我的GameActivity類。 我不確定問題是否在這里。

public class GameActivity extends BaseGameActivity {

            public static float CAMERA_WIDTH=0;
            public static float CAMERA_HEIGHT=0;
            private FollowCamera camera;

            SharedPreferences prefs;
            public SharedPreferences.Editor editor;

            public GameHelper mHelper;
            public static Tracker easyTracker;
        public GoogleAnalytics analytics;

            public void setHighScore(int score) {
                SharedPreferences.Editor settingsEditor = prefs.edit();
                settingsEditor.putInt(Constants.KEY_HISCORE, score);
                settingsEditor.apply();
            }

            public int getHighScore() {
                    return prefs.getInt(Constants.KEY_HISCORE, 0);
            }


            @Override
            public Engine onCreateEngine(EngineOptions pEngineOptions)
            {
                    return new LimitedFPSEngine(pEngineOptions, 60);
            }

            @Override
            public boolean onKeyDown(int keyCode, @NonNull KeyEvent event)
            {  
                if (keyCode == KeyEvent.KEYCODE_BACK)
                {
                    SceneManager.getInstance().getCurrentScene().onBackKeyPressed();
                }
                return false;
            }

            @Override
            public EngineOptions onCreateEngineOptions() {
                    ScreenDimentions();
                    prefs = PreferenceManager.getDefaultSharedPreferences(this);
                    camera = new FollowCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
                    EngineOptions engineOption = new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.camera);
                    engineOption.getAudioOptions().setNeedsMusic(true);
                    engineOption.getAudioOptions().setNeedsSound(true);
                    engineOption.getRenderOptions().getConfigChooserOptions().setRequestedMultiSampling(true);
                    engineOption.setWakeLockOptions(WakeLockOptions.SCREEN_ON);
                    engineOption.getTouchOptions().setNeedsMultiTouch(true);
                    return engineOption;
            }

            public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws IOException {
                    ResourcesManager.prepareManager(mEngine, this, camera, getVertexBufferObjectManager());
                    pOnCreateResourcesCallback.onCreateResourcesFinished();
            }

            @Override
            public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
                            throws IOException {
                    SceneManager.getInstance().createSplashScene(pOnCreateSceneCallback);
            }

            @Override
            public void onPopulateScene(Scene pScene,
                            OnPopulateSceneCallback pOnPopulateSceneCallback)
                            throws IOException {
                    mEngine.registerUpdateHandler(new TimerHandler(2f, new ITimerCallback() {

                            @Override
                            public void onTimePassed(TimerHandler pTimerHandler) {
                                    mEngine.unregisterUpdateHandler(pTimerHandler);
                                    SceneManager.getInstance().createGameScene();
                            }
                    }));
                    pOnPopulateSceneCallback.onPopulateSceneFinished();
            }

            @Override
            public synchronized void onResumeGame() {
                    super.onResumeGame();
            }

            @Override
            public synchronized void onPauseGame() {
                super.onPauseGame();
            }

            public static float Screen_width_inch;
            public static float Screen_height_inch;
            public static float Screen_width_cm;
            public static float Screen_height_cm;
            public static int Screen_width_pixels;
            public static int Screen_height_pixels;
            public static float Screen_dpi;
//            public static float Screen_size_inch;
            public static float density;
            public void ScreenDimentions(){
                final DisplayMetrics dm = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(dm);
                Screen_dpi = dm.scaledDensity;
                density = dm.density;
                Screen_width_pixels = dm.widthPixels;
                Screen_height_pixels = dm.heightPixels;
                CAMERA_WIDTH = Screen_width_pixels;
                CAMERA_HEIGHT = Screen_height_pixels;
                Screen_width_inch = 1.00f*dm.widthPixels/(1.00f*dm.xdpi);
                Screen_height_inch = 1.00f*dm.heightPixels/(1.00f*dm.ydpi);
                Screen_width_cm = Screen_width_inch*2.56f;
                Screen_height_cm = Screen_height_inch*2.56f;
            }
          @Override
          public void onDestroy()
          {
                  super.onDestroy();
          }
          @Override
          public void onResume() {
            super.onResume();
            if(mHelper==null){
              mHelper = new GameHelper(this, GameHelper.CLIENT_GAMES);
            }
            if (adView != null) {
                adView.resume();
            }
          }
          @Override
          public void onPause() {
            super.onPause();
            if (adView != null) {
                  adView.pause();
            }
          }
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);

                analytics = GoogleAnalytics.getInstance(this);
//        analytics.getLogger().setLogLevel(Logger.LogLevel.VERBOSE);
                analytics.setLocalDispatchPeriod(1800);

                easyTracker = analytics.newTracker("ID");
                easyTracker.enableAdvertisingIdCollection(true);
                easyTracker.enableAutoActivityTracking(true);
                easyTracker.enableExceptionReporting(true);

                mHelper = new GameHelper(this, GameHelper.CLIENT_GAMES);
//                mHelper.enableDebugLog(true, "GameHelper");
                editor = prefs.edit();
                GameHelperListener listener = new GameHelper.GameHelperListener() {
                    @Override
                    public void onSignInSucceeded() {
                        // handle sign-in succeess
                        Log.d("auth", "sign in successful");
                        editor.putBoolean("firstSignIn", true);
                        editor.apply();
                        if(prefs.getInt("autoSignIn", 0)==1){
                            trackEvent("social", "auth", "first_login");
                        }
                        trackEvent("social", "auth", "googlt_login");
                    }
                    @Override
                    public void onSignInFailed() {
                        // handle sign-in failure (e.g. show Sign In button)
                        Log.d("leaderboard","sign in failed");
                    }

                };
                mHelper.setup(listener);
                mInterstitialAd = new InterstitialAd(this);
                mInterstitialAd.setAdUnitId("ID");
             // Create ad request.
                AdRequest intestitialAdRequest = new AdRequest.Builder().build();
             // Begin loading your interstitial.
                mInterstitialAd.loadAd(intestitialAdRequest);
                mInterstitialAd.setAdListener(new AdListener() {
                    @Override
                    public void onAdLoaded() {
//                        Toast.makeText(MyActivity.this,
//                                "The interstitial is loaded", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onAdClosed() {
                        // Proceed to the next level.
//                        goToNextLevel();
                        AdRequest intestitialAdRequest = new AdRequest.Builder().build();
                        mInterstitialAd.loadAd(intestitialAdRequest);
                    }
                });
            }

            public static void displayInterstitial() {
                if (mInterstitialAd.isLoaded()) {
                    mInterstitialAd.show();
                }
            }
            @Override
            protected void onStart() {
                super.onStart();
                if(prefs.getInt("autoSignIn", 0)==0){
                    mHelper.beginUserInitiatedSignIn();
                    mHelper.onStart(this);
                    editor.putInt("autoSignIn", 1);
                    editor.commit();
                    trackEvent("social", "auth", "autoSignIn_shown");
                }
                if (prefs.getBoolean("firstSignIn", false)) {
////                    // auto sign in
                    if(!mHelper.isSignedIn()){
                        mHelper.getApiClient().connect();
                    }
                }

            }

            @Override
            protected void onStop() {
                super.onStop();
            }

            @Override
            protected void onActivityResult(int request, int response, Intent data) {
                super.onActivityResult(request, response, data);
                mHelper.onActivityResult(request, response, data);
            }

//          Banner ad through google api
            public static AdView adView;
            public static AdRequest adRequest;
            public static FrameLayout frameLayout;
            public static FrameLayout.LayoutParams adViewLayoutParams;
            public static InterstitialAd mInterstitialAd;

            @Override
            protected void onSetContentView() {

                frameLayout = new FrameLayout(this);
                final FrameLayout.LayoutParams frameLayoutLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
//                Log.d("Ads","frame layout params are "+frameLayoutLayoutParams.width+" and "+frameLayoutLayoutParams.height);
                adView = new AdView(this);
                new AdView(this);
                adView.setAdSize(AdSize.BANNER);

                adView.setAdUnitId("ID");
//                Log.d("Ads","AdView banner size is "+adView.getWidth()+" and "+adView.getHeight());
                adViewLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.TOP | Gravity.CENTER_HORIZONTAL);
//                Log.d("Ads","ad layout params are "+adViewLayoutParams.width+" and "+adViewLayoutParams.height);
                this.mRenderSurfaceView = new RenderSurfaceView(this);
                mRenderSurfaceView.setRenderer(this.mEngine, this);
                    adRequest = new AdRequest.Builder()
                            .addTestDevice("TEST_ID")
                            .addTestDevice("TEST_ID")
                            .build();

                    adView.setAdListener(new AdListener() {
                        @Override
                        public void onAdLoaded() {
                            frameLayout.removeView(adView);  // added to remove addview error
                            //                        frameLayout.addView(mRenderSurfaceView, frameLayoutLayoutParams);
                            frameLayout.addView(adView,adViewLayoutParams);
                            // Save app state before going to the ad overlay.
                        }
                    });
                final FrameLayout.LayoutParams surfaceViewLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
                frameLayout.addView(this.mRenderSurfaceView, surfaceViewLayoutParams);
                this.setContentView(frameLayout, frameLayoutLayoutParams);
                    adView.loadAd(adRequest);
            }

        // Event Tracking
        public void trackEvent(String category,String action,String label){
            try {
                easyTracker.send(new HitBuilders.EventBuilder()
                        .setCategory(category)
                        .setAction(action)
                        .setLabel(label)
                        .build());
            }
            catch (Exception error){

            }
        }
    }

問題nr。 1:如果OpenGL上下文丟失,則會發生這種情況。 您可以強制上下文保持活動。 它仍然可以丟失,但很少見。 您可以處理丟失的事件並重新加載紋理(如果您未進行任何更改,引擎實際上應該自動完成此操作)。 您可以在AndEngine論壇上找到大量有關如何實現此目標的文章。

問題nr。 2:是否調用引擎的所有恢復/暫停方法以確保引擎正確地暫停?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM