Skip to content

Commit 3b4df19

Browse files
committed
added ShakeDetectSampleApplication
1 parent ca0f444 commit 3b4df19

File tree

1 file changed

+15
-9
lines changed
  • android/ShakeDetectSampleApplication/app/src/main/java/com/hrupin/sample/shakedetect

1 file changed

+15
-9
lines changed

android/ShakeDetectSampleApplication/app/src/main/java/com/hrupin/sample/shakedetect/MainActivity.java

+15-9
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,24 @@ public class MainActivity extends Activity implements SensorEventListener, View.
2929
protected void onCreate(Bundle savedInstanceState) {
3030
super.onCreate(savedInstanceState);
3131
setContentView(R.layout.activity_main);
32+
}
3233

33-
background = (RelativeLayout)findViewById(R.id.rlBackground);
34-
background.setOnClickListener(this);
35-
text = (TextView)findViewById(R.id.text);
36-
resetScreenUI();
34+
@Override
35+
protected void onResume() {
36+
super.onResume();
37+
if(sensorMgr == null) {
38+
background = (RelativeLayout) findViewById(R.id.rlBackground);
39+
background.setOnClickListener(this);
40+
text = (TextView) findViewById(R.id.text);
41+
resetScreenUI();
3742

38-
sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
39-
boolean accelSupported = sensorMgr.registerListener(this, sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
43+
sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
44+
boolean accelSupported = sensorMgr.registerListener(this, sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
4045

41-
if (!accelSupported) {
42-
// on accelerometer on this device
43-
sensorMgr.unregisterListener(this, sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER));
46+
if (!accelSupported) {
47+
// on accelerometer on this device
48+
sensorMgr.unregisterListener(this, sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER));
49+
}
4450
}
4551
}
4652

0 commit comments

Comments
 (0)