Using Sensor : Sensor « Hardware « Android






Using Sensor

   
package app.test;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.provider.Settings;

public class Test extends Activity implements SensorEventListener {
    private WakeLock mWakelock;
    private PowerManager mPwrMgr;
    private WakeLock mTurnBackOn = null;
    Handler handler = new Handler();
  private SensorManager mMgr;
    private Sensor mAccel;
    private BufferedWriter mLog;
  final private SimpleDateFormat mTimeFormat = new SimpleDateFormat("HH:mm:ss - ");
  private int mSavedTimeout;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mMgr = (SensorManager) this.getSystemService(SENSOR_SERVICE);
        mAccel = mMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        try {
            String filename = Environment.getExternalStorageDirectory().getAbsolutePath() +
            "/accel.log";
            mLog = new BufferedWriter(new FileWriter(filename, true));
        }
        catch(Exception e) {
          e.printStackTrace();
          finish();
        }
        mPwrMgr = (PowerManager) this.getSystemService(POWER_SERVICE);
        mWakelock = mPwrMgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Accel");
        mWakelock.acquire();
        try {
          mSavedTimeout = Settings.System.getInt(getContentResolver(), 
              Settings.System.SCREEN_OFF_TIMEOUT);
        }
        catch(Exception e) {
          mSavedTimeout = 120000;  // 2 minutes
        }
        Settings.System.putInt(getContentResolver(), 
            Settings.System.SCREEN_OFF_TIMEOUT, -1);  // always on
    }

    public BroadcastReceiver mReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
                handler.post(new Runnable() {
          public void run() {
            if(mTurnBackOn != null)
              mTurnBackOn.release();
                    mTurnBackOn = mPwrMgr.newWakeLock(
                            PowerManager.SCREEN_DIM_WAKE_LOCK |
                                    PowerManager.ACQUIRE_CAUSES_WAKEUP,
                            "AccelOn");
                    mTurnBackOn.acquire();
          }});
            }
        }
    };

    @Override
    protected void onStart() {
        mMgr.registerListener(this, mAccel, SensorManager.SENSOR_DELAY_NORMAL);
        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
        registerReceiver(mReceiver, filter);
      super.onStart();
    }

    @Override
    protected void onStop() {
        mMgr.unregisterListener(this, mAccel);
        unregisterReceiver(mReceiver);
        try {
      mLog.flush();
    } catch (IOException e) {
    }
      super.onStop();
    }

    @Override
    protected void onDestroy() {
      try {
          mLog.flush();
          mLog.close();
      }
      catch(Exception e) {
      }
        Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, mSavedTimeout);
        mWakelock.release();
        if(mTurnBackOn != null)
            mTurnBackOn.release();
        super.onDestroy();
    }
  public void onAccuracyChanged(Sensor sensor, int accuracy) {
  }
  public void onSensorChanged(SensorEvent event) {
    writeLog("Got a sensor event: " + event.values[0] + ", " +
        event.values[1] + ", " + event.values[2]);
  }
  private void writeLog(String str) {
    try {
        Date now = new Date();
        mLog.write(mTimeFormat.format(now));
      mLog.write(str);
      mLog.write("\n");
    }
    catch(IOException ioe) {
      ioe.printStackTrace();
    }
  }
}

   
    
    
  








Related examples in the same category

1.Accelero meter Sensor
2.Compass sensor
3.Gravity sensor
4.Sensor.TYPE_GYROSCOPE
5.Light Sensor
6.PROXIMITY Sensor
7.Sensor List
8.Temperature Sensor
9.Sensor Changed event and information
10.Sensor Event Listener
11.Speed sensor
12.Sensor Test
13.Displays the values of the acceleration sensor graphically
14.Compass Activity
15.Accelerometer API
16.Using the accelerometer to integrate the device's acceleration to a position using the Verlet method.