Question

I can't find where the problem. When I shake the phone what it's should do is CHange the text of the textview, but Instead when I shake it it Crashes. What's the problem In my code? I've tries to look on the logcat but I'm not skilled yet to Understand something from it (especially cause my native language is not English ). THanks! Main Activity

package nir.rauch.accelrate;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Vibrator;
import android.widget.TextView;

public class MainActivity extends Activity
{
  private ShakeListener mShaker;
public TextView text;
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
final TextView text =(TextView)findViewById(R.id.textView1);
    final Vibrator vibe = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
mShaker = new ShakeListener(this);
mShaker.setOnShakeListener(new ShakeListener.OnShakeListener () {
  public void onShake()
  {
    vibe.vibrate(100);
   text.setText("" + "what a suprise! after the shaking The text has been changed and it also "
        + "Vibrates , so AMAZING!");
  }
});
  }

  @Override
  public void onResume()
  {
    mShaker.resume();
    super.onResume();
  }
  @Override
  public void onPause()
  {
    mShaker.pause();
    super.onPause();
  }
}

The Shaker activity

    package nir.rauch.accelrate;

import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.content.Context;
import java.lang.UnsupportedOperationException;

public class ShakeListener implements SensorListener 
{
  private static final int FORCE_THRESHOLD = 350;
  private static final int TIME_THRESHOLD = 100;
  private static final int SHAKE_TIMEOUT = 500;
  private static final int SHAKE_DURATION = 1000;
  private static final int SHAKE_COUNT = 3;

  private SensorManager mSensorMgr;
  private float mLastX=-1.0f, mLastY=-1.0f, mLastZ=-1.0f;
  private long mLastTime;
  private OnShakeListener mShakeListener;
  private Context mContext;
  private int mShakeCount = 0;
  private long mLastShake;
  private long mLastForce;

  public interface OnShakeListener
  {
    public void onShake();
  }

  public ShakeListener(Context context) 
  { 
    mContext = context;
    resume();
  }

  public void setOnShakeListener(OnShakeListener listener)
  {
    mShakeListener = listener;
  }

  public void resume() {
    mSensorMgr = (SensorManager)mContext.getSystemService(Context.SENSOR_SERVICE);
    if (mSensorMgr == null) {
      throw new UnsupportedOperationException("Sensors not supported");
    }
    boolean supported = mSensorMgr.registerListener(this, SensorManager.SENSOR_ACCELEROMETER, SensorManager.SENSOR_DELAY_GAME);
    if (!supported) {
      mSensorMgr.unregisterListener(this, SensorManager.SENSOR_ACCELEROMETER);
      throw new UnsupportedOperationException("Accelerometer not supported");
    }
  }

  public void pause() {
    if (mSensorMgr != null) {
      mSensorMgr.unregisterListener(this, SensorManager.SENSOR_ACCELEROMETER);
      mSensorMgr = null;
    }
  }

  public void onAccuracyChanged(int sensor, int accuracy) { }

  public void onSensorChanged(int sensor, float[] values) 
  {
    if (sensor != SensorManager.SENSOR_ACCELEROMETER) return;
    long now = System.currentTimeMillis();

    if ((now - mLastForce) > SHAKE_TIMEOUT) {
      mShakeCount = 0;
    }

    if ((now - mLastTime) > TIME_THRESHOLD) {
      long diff = now - mLastTime;
      float speed = Math.abs(values[SensorManager.DATA_X] + values[SensorManager.DATA_Y] + values[SensorManager.DATA_Z] - mLastX - mLastY - mLastZ) / diff * 10000;
      if (speed > FORCE_THRESHOLD) {
        if ((++mShakeCount >= SHAKE_COUNT) && (now - mLastShake > SHAKE_DURATION)) {
          mLastShake = now;
          mShakeCount = 0;
          if (mShakeListener != null) { 
            mShakeListener.onShake(); 
          }
        }
        mLastForce = now;
      }
      mLastTime = now;
      mLastX = values[SensorManager.DATA_X];
      mLastY = values[SensorManager.DATA_Y];
      mLastZ = values[SensorManager.DATA_Z];
    }
  }

}

The XML file:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40sp"
        android:text="Does the Accelorometer works? Shake it and found out!" />


</RelativeLayout>

my LOGCAT:

    04-12 18:49:15.831: E/SensorManager(5343): Exception dispatching input event.
04-12 18:49:15.831: E/AndroidRuntime(5343): FATAL EXCEPTION: main
04-12 18:49:15.831: E/AndroidRuntime(5343): java.lang.SecurityException: Requires VIBRATE permission
04-12 18:49:15.831: E/AndroidRuntime(5343):     at android.os.Parcel.readException(Parcel.java:1431)
04-12 18:49:15.831: E/AndroidRuntime(5343):     at android.os.Parcel.readException(Parcel.java:1385)
04-12 18:49:15.831: E/AndroidRuntime(5343):     at android.os.IVibratorService$Stub$Proxy.vibrateMagnitude(IVibratorService.java:342)
04-12 18:49:15.831: E/AndroidRuntime(5343):     at android.os.SystemVibrator.vibrate(SystemVibrator.java:115)
04-12 18:49:15.831: E/AndroidRuntime(5343):     at android.os.SystemVibrator.vibrate(SystemVibrator.java:83)
04-12 18:49:15.831: E/AndroidRuntime(5343):     at nir.rauch.accelrate.MainActivity$1.onShake(MainActivity.java:25)
04-12 18:49:15.831: E/AndroidRuntime(5343):     at nir.rauch.accelrate.ShakeListener.onSensorChanged(ShakeListener.java:79)
04-12 18:49:15.831: E/AndroidRuntime(5343):     at android.hardware.LegacySensorManager$LegacyListener.onSensorChanged(LegacySensorManager.java:274)
04-12 18:49:15.831: E/AndroidRuntime(5343):     at android.hardware.SystemSensorManager$SensorEventQueue.dispatchSensorEvent(SystemSensorManager.java:467)
04-12 18:49:15.831: E/AndroidRuntime(5343):     at android.os.MessageQueue.nativePollOnce(Native Method)
04-12 18:49:15.831: E/AndroidRuntime(5343):     at android.os.MessageQueue.next(MessageQueue.java:132)
04-12 18:49:15.831: E/AndroidRuntime(5343):     at android.os.Looper.loop(Looper.java:124)
04-12 18:49:15.831: E/AndroidRuntime(5343):     at android.app.ActivityThread.main(ActivityThread.java:5419)
04-12 18:49:15.831: E/AndroidRuntime(5343):     at java.lang.reflect.Method.invokeNative(Native Method)
04-12 18:49:15.831: E/AndroidRuntime(5343):     at java.lang.reflect.Method.invoke(Method.java:525)
04-12 18:49:15.831: E/AndroidRuntime(5343):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
04-12 18:49:15.831: E/AndroidRuntime(5343):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
04-12 18:49:15.831: E/AndroidRuntime(5343):     at dalvik.system.NativeStart.main(Native Method)
04-12 18:55:45.396: E/SensorManager(6408): Exception dispatching input event.
04-12 18:55:45.401: E/AndroidRuntime(6408): FATAL EXCEPTION: main
04-12 18:55:45.401: E/AndroidRuntime(6408): java.lang.SecurityException: Requires VIBRATE permission
04-12 18:55:45.401: E/AndroidRuntime(6408):     at android.os.Parcel.readException(Parcel.java:1431)
04-12 18:55:45.401: E/AndroidRuntime(6408):     at android.os.Parcel.readException(Parcel.java:1385)
04-12 18:55:45.401: E/AndroidRuntime(6408):     at android.os.IVibratorService$Stub$Proxy.vibrateMagnitude(IVibratorService.java:342)
04-12 18:55:45.401: E/AndroidRuntime(6408):     at android.os.SystemVibrator.vibrate(SystemVibrator.java:115)
04-12 18:55:45.401: E/AndroidRuntime(6408):     at android.os.SystemVibrator.vibrate(SystemVibrator.java:83)
04-12 18:55:45.401: E/AndroidRuntime(6408):     at nir.rauch.accelrate.MainActivity$1.onShake(MainActivity.java:25)
04-12 18:55:45.401: E/AndroidRuntime(6408):     at nir.rauch.accelrate.ShakeListener.onSensorChanged(ShakeListener.java:79)
04-12 18:55:45.401: E/AndroidRuntime(6408):     at android.hardware.LegacySensorManager$LegacyListener.onSensorChanged(LegacySensorManager.java:274)
04-12 18:55:45.401: E/AndroidRuntime(6408):     at android.hardware.SystemSensorManager$SensorEventQueue.dispatchSensorEvent(SystemSensorManager.java:467)
04-12 18:55:45.401: E/AndroidRuntime(6408):     at android.os.MessageQueue.nativePollOnce(Native Method)
04-12 18:55:45.401: E/AndroidRuntime(6408):     at android.os.MessageQueue.next(MessageQueue.java:132)
04-12 18:55:45.401: E/AndroidRuntime(6408):     at android.os.Looper.loop(Looper.java:124)
04-12 18:55:45.401: E/AndroidRuntime(6408):     at android.app.ActivityThread.main(ActivityThread.java:5419)
04-12 18:55:45.401: E/AndroidRuntime(6408):     at java.lang.reflect.Method.invokeNative(Native Method)
04-12 18:55:45.401: E/AndroidRuntime(6408):     at java.lang.reflect.Method.invoke(Method.java:525)
04-12 18:55:45.401: E/AndroidRuntime(6408):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
04-12 18:55:45.401: E/AndroidRuntime(6408):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
04-12 18:55:45.401: E/AndroidRuntime(6408):     at dalvik.system.NativeStart.main(Native Method)
Was it helpful?

Solution

as your logcat said,

java.lang.SecurityException: Requires VIBRATE permission

you need Vibrate permission. so you need add following line into manifest file:

 <uses-permission android:name="android.permission.VIBRATE"/>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top