// 播放闹钟音乐
MediaPlayer m = null;
if(!alarmStart) {
m = MediaPlayer.create(MainActivity.this, R.raw.alarm);
m.setLooping(true);
m.start();
alarmStart = true;
}
// 一分钟未醒,获取最大音量,并设置为最大
while(!wakeup) {
try {
sleep(1000);
cnt++;
AudioManager am=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
int maxVolume = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
if(cnt > 60) {
am.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, AudioManager.FLAG_PLAY_SOUND);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void MoveThread() {
SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
Sensor s = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
SensorEventListener el = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent e) {
if(userCheck) {
if(e.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
float x = e.values[0];
float y = e.values[1];
float z = e.values[2];
if ((Math.abs(x)>12||Math.abs(y)>12||Math.abs(z)>12)) {
isReset = true;
running = false;
unMove = false;
start.callOnClick();
} else {
if(!startEnd) {
running = true;
}
}
}
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
};
sm.registerListener(el,s,SensorManager.SENSOR_DELAY_UI);
}
KeyguardManager mKeyguardManager = (KeyguardManager) getSystemService (Context. KEYGUARD_SERVICE);
boolean flag = mKeyguardManager.inKeyguardRestrictedInputMode();
all.setBackgroundResource(0);
all.setBackgroundColor(R.color.sleep);
这个项目还有很多不成熟的地方,判断入睡逻辑不够完备,并且如果可以根据睡眠时间长度动态响应,会更好。
使用线程过多,对资源有一定的消耗,并且线程间通信逻辑比较混乱,不利于修改。