我想将信息(1整数)从surfaceview传递给activity,但抛出intent和putextra我不能这样做,抛出surfaceview.fail我得到0.代码有主要的和平:
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 fail会在每一帧之后增加,请帮助,我如何在single_result类中获得失败的最终值。
答案 0 :(得分:0)
创建一个监听器接口
public interface GameCallback{
public void onEvent(int i);
}
让你的活动实现GameCallback
public class YourActivity extends Activity implements GameCallback{...
您将从游戏活动的onEvent()方法中获取Surface中的整数
@Override
public void onEvent(int i){
// handle your integer here
}
在创建游戏后的某处添加到您的活动方法:
mGame.setGameCallback(this);
其中mGame是你的表面视图
添加到您的游戏课程:
private GameCallback mGameCallback;
public void setGameCallback(GameCallback cb){
this.mGameCallback = cb;
}
如果您想向活动发送消息,请添加:
if(mGameCallback!=null){
mGameCallback(someNumber);
}
就是这样