import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class AgileBuddyActivity extends Activity {
private AgileBuddyView mAgileBuddyView;
private SensorManager mSensorManager;
private Sensor mSensor;
private SensorEventListener mSensorEventListener;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); (1)
// 取消标题
requestWindowFeature(Window.FEATURE_NO_TITLE);
// 禁止屏幕休眠
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
// 全屏幕
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
mAgileBuddyView = (AgileBuddyView) findViewById(R.id.agile_buddy);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorEventListener = new SensorEventListener() {
public void onSensorChanged(SensorEvent e) {
mAgileBuddyView.handleMoving(e.values[SensorManager.DATA_X]);
}
public void onAccuracyChanged(Sensor s, int accuracy) {
}
};
// 注册重力感应监听
mSensorManager.registerListener(mSensorEventListener, mSensor,
SensorManager.SENSOR_DELAY_GAME);
}
@Override
public void finish() {
// 注销重力感应监听
mSensorManager.unregisterListener(mSensorEventListener, mSensor);
super.finish(); (2)
}
}
android 中经常不知道复写父类的方法时,super.xx(ArgumentsSets)应该放在哪里,或是干脆可以不要?
上面这段代码给了我一个很有说服力的解释。android底层会自己去调用这些回调方法,当然父类做了一
些事情。而我们也想再在这些方法里面添加一些操作,应该放在哪里取决于具体场景。
(1)中,创建一个Activity时,我们的代码要等系统创建好了才能够很好的执行,所以应该放在
super.onCreate(savedInstance)后面;
(2)中,我们的代码希望在Activity销毁之前调用,所以需要放在super.finish()之前调用。所以我们的操作放
在super.finish()前面。
(super.finish()会调用onDestroy方法)