我需要在意图中传递布尔值,并在按下返回按钮时将其传回。目标是设置布尔值并使用条件语句来防止在检测到onShake事件时多次启动新意图。我本来想使用SharedPreferences,但它似乎与我的onClick代码不兼容,而我也不知道如何解决这个问题。欢迎任何建议!
public class MyApp extends Activity {
private SensorManager mSensorManager;
private ShakeEventListener mSensorListener;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSensorListener = new ShakeEventListener();
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensorManager.registerListener(mSensorListener,
mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_UI);
mSensorListener.setOnShakeListener(new ShakeEventListener.OnShakeListener() {
public void onShake() {
// This code is launched multiple times on a vigorous
// shake of the device. I need to prevent this.
Intent myIntent = new Intent(MyApp.this, NextActivity.class);
MyApp.this.startActivity(myIntent);
}
});
}
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(mSensorListener,mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_UI);
}
@Override
protected void onStop() {
mSensorManager.unregisterListener(mSensorListener);
super.onStop();
}}