我是安卓新手。我写了一个程序,球通过加速度传感器移动,这很好用。我为此使用 Timer 和 schedule 方法(schedule(mTimerTask, delay,period);) 我的屏幕上有两个按钮,我希望当用户单击正按钮时,周期和延迟值增加,当单击负按钮时,这些值减少。我写了这段代码,但是 onClick 方法中的更改值在 onResume 方法中没有修复。我知道 onResume 方法被称为生命周期的一部分,但我不知道如何解决我的问题!请帮助我:)谢谢
public class MainActivity extends Activity {
float result=0;
BallView ball=null;
Handler redrawHandler=new Handler();
//
Timer mTimer;
TimerTask mTimerTask;
//
int mSrcWidth,mSrcHeight;
PointF mBallPos,mBallSpd;
//
Button positiveBtn;
Button negativeBtn;
//
int delay=10;
int period=10;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
positiveBtn=(Button)findViewById(R.id.positiveBtn);
negativeBtn=(Button)findViewById(R.id.negativeBtn);
Log.e("onclick", "in oncreate");
getWindow().setFlags(0xFFFFFFFF, LayoutParams.FLAG_FULLSCREEN|LayoutParams.FLAG_KEEP_SCREEN_ON);
final FrameLayout frame=(FrameLayout)findViewById(R.id.main_view);
//
Display display=getWindowManager().getDefaultDisplay();
mSrcWidth=display.getWidth();
mSrcHeight=display.getHeight();
mBallPos=new PointF();
mBallSpd=new PointF();
mBallPos.x=mSrcWidth/2;
mBallPos.y=mSrcHeight/2;
mBallSpd.x=0;
mBallSpd.y=0;
ball=new BallView(this,mBallPos.x,mBallPos.y,10);
frame.addView(ball);
//
ball.invalidate();
positiveBtn.setOnClickListener(onClickListener);
negativeBtn.setOnClickListener(onClickListener);
SensorManager sm=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
sm.registerListener(new SensorEventListener(){
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
//
mBallSpd.x=-event.values[0];
mBallSpd.y=event.values[1];
}
//
},sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0), SensorManager.SENSOR_DELAY_NORMAL);
Log.e("onclick", "in onsensor");
}
private OnClickListener onClickListener=new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.negativeBtn:
if(delay<=5&&period<=5)
delay=period=5;
else{
delay=delay-10;
period=period-10;
}
break;
case R.id.positiveBtn:
if(delay>=50&&period>=50)
delay=period=150;
else{
delay=delay+10;
period=period+10;
}
break;
}
Log.e("delay","="+ delay);
Log.e("period","="+ period);
}
} ;
@Override
public void onPause(){
mTimer.cancel();
mTimer=null;
mTimerTask=null;
super.onPause();
}
@Override
public void onResume(){
mTimer=new Timer();
mTimerTask=new TimerTask(){
public void run(){
//Log.e("in ", "onResume()");
//positiveBtn.setOnClickListener(onClickListener);
//negativeBtn.setOnClickListener(onClickListener);
mBallPos.x+=mBallSpd.x;
mBallPos.y+=mBallSpd.y;
if(mBallPos.x>=mSrcWidth)
mBallPos.x=mSrcWidth;
if(mBallPos.y>=mSrcHeight)
mBallPos.y=mSrcHeight;
if(mBallPos.x<=0)
mBallPos.x=0;
if(mBallPos.y<=0)
mBallPos.y=0;
ball.x=mBallPos.x;
ball.y=mBallPos.y;
redrawHandler.post(new Runnable(){
public void run() {
// TODO Auto-generated method stub
ball.invalidate();
}
});
}
};
mTimer.schedule(mTimerTask, delay,period);
super.onResume();
}