Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think that a Timer would work nicely for you. Perhaps something like this, where you cancel the timer and start a new timer every time you detect motion:</p> <p>define your timer in your activity (and make sure you cancel it when you leave the activity)</p> <pre><code>Timer lastTouchedTimer = null; </code></pre> <p>Then change your onSensorChanged like this:</p> <pre><code> public void onSensorChanged(SensorEvent event) { float x = event.values[0]; float y = event.values[1]; if(x &gt; -0.1 &amp;&amp; x &lt; 0.1 &amp;&amp; y &gt; -0.1 &amp;&amp; y &lt; 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); } } </code></pre> <p>Here is an activity, which I use in a sample activity.</p> <pre><code>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 &gt; -0.1 &amp;&amp; x &lt; 0.1 &amp;&amp; y &gt; -0.1 &amp;&amp; y &lt; 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. } }; } } </code></pre> <p>It uses a very simple xml file:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; &lt;TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Heallo World, MyActivity" /&gt; &lt;/LinearLayout&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload