2012-05-12 5 views
0

귀하의 도움과 인터넷상의 일부 가이드를 통해이 코드를 폐기 할 수 있습니다. 하지만 몇 가지 문제점을 안고 있지만, 응용 프로그램은 내 생각에 따라 전화가 흔들릴 때 미디어 플레이어를 시작하고 한 번 시작해야하지만 이제는 전화가 흔들릴 때마다 미디어 플레이어가 시작됩니다. 또한, 나는 그것을 원하는 응용 프로그램이 작동하고 전화가 흔들리는 화면 오프 미디어 플레이어를 시작하고 화면이 단 한 번 뭔가를 시작 부울을 추가하려면가속도계 센서 및 미디어 플레이어

public class SensorTestActivity extends Activity implements SensorEventListener { 
private SensorManager sensorManager; 
private boolean color = false; 
private View view; 
private long lastUpdate; 
MediaPlayer mMediaPlayer; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
requestWindowFeature(Window.FEATURE_NO_TITLE); 
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
WindowManager.LayoutParams.FLAG_FULLSCREEN); 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
view = findViewById(R.id.textView); 
view.setBackgroundColor(Color.BLUE); 
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); 
sensorManager.registerListener(this, 
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), 
SensorManager.SENSOR_DELAY_NORMAL); 
lastUpdate = System.currentTimeMillis(); 
} 

@Override 
public void onSensorChanged(SensorEvent event) { 
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { 
getAccelerometer(event); 
} 
} 
private void getAccelerometer(SensorEvent event) { 
float[] values = event.values; 
// Movement 
float x = values[0]; 
float y = values[1]; 
float z = values[2]; 

float accelationSquareRoot = (x * x + y * y + z * z)/(SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH); 
long actualTime = System.currentTimeMillis(); 
if (accelationSquareRoot >= 2) // 
{ 
if (actualTime - lastUpdate < 200) { 
return; 
} 
lastUpdate = actualTime; 
Toast.makeText(this, "Device was shuffed", Toast.LENGTH_SHORT).show(); 
if (color) { 
view.setBackgroundColor(Color.GREEN); 

try { 

mMediaPlayer = MediaPlayer.create(getBaseContext(), R.raw.mymusic); 

mMediaPlayer.setLooping(false); 
mMediaPlayer.start(); 
mMediaPlayer.setOnCompletionListener(new 
OnCompletionListener() { 
public void onCompletion(MediaPlayer arg0) { 
// if (mMediaPlayer != null) { 
// mMediaPlayer.release(); 
// mMediaPlayer = null; 
// } 
} 
}); 
} catch (Exception e) { 
if (mMediaPlayer != null) { 
mMediaPlayer.release(); 
mMediaPlayer = null; 
} 
}     
} 
} else { 
view.setBackgroundColor(Color.RED); 
} 
color = !color; 
} 

@Override 
public void onAccuracyChanged(Sensor sensor, int accuracy) { 
} 

@Override 
protected void onResume() { 
super.onResume(); 
// register this class as a listener for the orientation and 
// accelerometer sensors 
sensorManager.registerListener(this, 
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), 
SensorManager.SENSOR_DELAY_NORMAL); 
} 

@Override 
protected void onPause() { 
// unregister listener 
super.onPause(); 
sensorManager.unregisterListener(this); 
} 
} 

답변

0

켜집니다.

boolean startOnce = true; 

if (startOnce) { 
.... 
startOnce = false; 
} 
+0

미안하지만 내 코드에서 "if"구조로 부울 변수를 삽입해야합니까? – bisssi