2012-07-24 82 views
2

我有一個簡單的應用程序,其中,我創建了一個SurfaceView類,然後在主活性,我創建的類的一個對象,然後加入此SurfaceView到的相對佈局和內容視圖設置爲相對佈局。恢復的Android活動

package com.my.game; 
import com.google.ads.Ad; 
import com.google.ads.AdListener; 
import com.google.ads.AdRequest; 
import com.google.ads.AdRequest.ErrorCode; 
import com.google.ads.AdSize; 
import com.google.ads.AdView; 
import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Matrix; 
import android.graphics.drawable.BitmapDrawable; 
import android.graphics.drawable.Drawable; 
import android.os.Bundle; 
import android.util.DisplayMetrics; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.Window; 
import android.widget.RelativeLayout; 

public class ShootLetters extends Activity { 

    private Panel panel; 
    private int newWidth; 
    private int newHeight; 

    private AdView adView; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Bitmap bg = BitmapFactory.decodeResource(this.getResources(), 
       R.drawable.shooting_background); 
     ; 

     int width = bg.getWidth(); 
     int height = bg.getHeight(); 

     DisplayMetrics metrics = new DisplayMetrics(); 
     getWindowManager().getDefaultDisplay().getMetrics(metrics); 

     newWidth = metrics.widthPixels; 

     newHeight = metrics.heightPixels; 
     float scaleWidth = ((float) newWidth)/width; 
     float scaleHeight = ((float) newHeight)/height; 
     // CREATE A MATRIX FOR THE MANIPULATION 
     Matrix matrix = new Matrix(); 
     // RESIZE THE BIT MAP 
     matrix.postScale(scaleWidth, scaleHeight); 

     Bitmap resizedBackGround = Bitmap.createBitmap(bg, 0, 0, width, height, 
       matrix, false); 

     Window win = getWindow(); 
     Drawable d = new BitmapDrawable(resizedBackGround); 
     win.setBackgroundDrawable(d); 

     panel = new Panel(this, newWidth, newHeight); 

     panel.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       if (event.getAction() == MotionEvent.ACTION_DOWN) { 
        panel.setClickX(event.getX()); 
        panel.setClickY(event.getY()); 
       } 
       return true; 
      } 

     }); 

     adView = new AdView(this, AdSize.BANNER, "admobidxxx222"); 

     RelativeLayout rl = new RelativeLayout(this); 
     rl.addView(panel); 
     rl.addView(adView); 

     setContentView(rl); 

     // Initiate a generic request to load it with an ad 
     adView.loadAd(new AdRequest()); 
    } 

    @Override 
    public void onDestroy() { 
     if (adView != null) { 
      adView.destroy(); 
     } 
     panel.stopThread(); 
     panel = null; 

     super.onDestroy(); 
    } 

} 

這適用於啓動和停止應用程序。

問題是,當其他活動都在這一層上,它處於一種不穩定的方式(在這一階段沒有必要細節,有時死機,有時缺少佈局對象的作品)的行爲。

我讀過,我應該執行onPause() & onResume()方法來處理這個。 我只需要暫停活動或在簡歷上恢復。無需持久數據。

我應該放什麼,在這種情況下,哪些方法?

感謝

回答

1

我想這是你的Panel那是surfaceview?我看到你正在onDestroy中使用它的線程,但這還不夠。你有關於實施onPause()onResume()現在我的想法(沒有能夠看到所有的代碼)正確的直覺是,你不處理與線程正確。在您的Panel類延伸surfaceview你應該有類似這兩個函數:

public void pause(){ 
     Loop = false; 
     while (true){ 
      try{ 
       panelThread.join();//end it 
      }catch (InterruptedException e){ 
       e.printStackTrace(); 
      } 
      break; 
     } 
     panelThread = null; 
    } 
    public void resume(){ 
     Loop = true; 
     panelThread = new Thread(this); 
     panelThread.start(); 
    } 

,如果你沒有在你的run()方法使用一個循環忽略循環的一部分。

然後,在您的活動的onPause()onResume()方法中,分別稱爲panel.pause()panel.resume()。這可以確保表面視圖線程以安全的方式處理。