我认为 Timer 很适合你。可能是这样的,每次检测到运动时取消计时器并启动新计时器:
在您的活动中定义您的计时器(并确保在您离开活动时取消它)
Timer lastTouchedTimer = null;
然后像这样更改您的 onSensorChanged:
public void onSensorChanged(SensorEvent event) {
float x = event.values[0];
float y = event.values[1];
if(x > -0.1 && x < 0.1 && y > -0.1 && y < 0.1) {
if (lastTouchedTimer == null) {
lastTouchedTimer = new Timer();
} else
lastTouchedTimer.cancel();
}
lastTouchedTimer.schedule(new TimerTask() {
@Override
public void run() {
try {
AssetFileDescriptor afd = getAssets().openFd("beep.wav");
mMediaplayer = new MediaPlayer();
mMediaplayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
mMediaplayer.prepare();
mMediaplayer.start();
mMediaplayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
//mp.setLooping(false);
mp.stop();
mp.release();
}
});
} catch(Exception e) {
e.printStackTrace();
}
}
}, 5000);
}
}
这是我在示例活动中使用的活动。
package com.example.test;
import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.media.MediaPlayer;
import android.os.Bundle;
import java.util.Timer;
import java.util.TimerTask;
public class MyActivity extends Activity {
Timer lastTouchedTimer;
MediaPlayer mMediaplayer;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SensorEventListener newListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
float x = sensorEvent.values[0];
float y = sensorEvent.values[1];
if(x > -0.1 && x < 0.1 && y > -0.1 && y < 0.1) {
if (lastTouchedTimer == null) {
lastTouchedTimer = new Timer();
} else
lastTouchedTimer.cancel();
}
lastTouchedTimer.schedule(new TimerTask() {
@Override
public void run() {
try {
AssetFileDescriptor afd = getAssets().openFd("beep.wav");
mMediaplayer = new MediaPlayer();
mMediaplayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
mMediaplayer.prepare();
mMediaplayer.start();
mMediaplayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
//mp.setLooping(false);
mp.stop();
mp.release();
}
});
} catch(Exception e) {
e.printStackTrace();
}
}
}, 5000);
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
//To change body of implemented methods use File | Settings | File Templates.
}
};
}
}
它使用一个非常简单的 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Heallo World, MyActivity"
/>
</LinearLayout>