2014-02-10 111 views
-1

我想從surfaceview傳遞信息(1個整數)到活動,但拋出intent和putextra我不能這樣做,拋出surfaceview.fail我得到0.有主要的代碼:從活動中的surfaceview獲取信息

public class Game extends SurfaceView { 
    int fail=0; 
    ... 
    protected void onDraw(Canvas canvas) 
     { if (sch==3){ 

       Context context = getContext(); 
       Intent intent = new Intent(context, Single_result.class); 

       context.startActivity(intent);} 
fail+=1; 
...(drawing) 

int failure在每幀後都會增加,請大家幫忙,我怎麼能在single_result類中得到最終的失敗值。

+0

請說明你需要什麼? – Shawn

回答

0

創建一個監聽接口

public interface GameCallback{ 
    public void onEvent(int i); 
} 

讓您的活動實施GameCallback

public class YourActivity extends Activity implements GameCallback{... 

你會得到你的整數面的onEvent()您的遊戲活動的方法

@Override 
public void onEvent(int i){ 
    // handle your integer here 
} 

在遊戲創建後添加到您的活動方法:

mGame.setGameCallback(this); 

其中MGAME是您surfaceview

添加到您的遊戲類:

private GameCallback mGameCallback; 

public void setGameCallback(GameCallback cb){ 
    this.mGameCallback = cb; 
} 

當你想將消息發送到您的活動添加:

if(mGameCallback!=null){ 
    mGameCallback(someNumber); 
} 

就是這樣