blogfiles.naver.net.png  

package kr.hews.sensorsample;

import! org.openintents.hardware.SensorManagerSimulator;
import! org.openintents.provider.Hardware;

import! android.app.Activity;
import! android.content.Intent;
import! android.content.res.Resources;
import! android.hardware.SensorListener;
import! android.hardware.SensorManager;
import! android.os.Bundle;
import! android.widget.TextView;

public class SensorSample extends Activity
{
  SensorListener mSensorListener = new SensorListener()
  {
    private int mOriValCount;
    private int mOriAccCount;
    private int mAccValCount;
    private int mAccAccCount;

    public void onAccuracyChanged(int sensor, int accuracy)
    {
      if (sensor == SensorManager.SENSOR_ORIENTATION)
      {
        ++mOriAccCount;
      }
      else if (sensor == SensorManager.SENSOR_ACCELEROMETER)
      {
        ++mAccAccCount;
      }
      setCount();
    }

    public void onSensorChanged(int sensor, float[] values)
    {
      if (sensor == SensorManager.SENSOR_ORIENTATION)
      {
        ++mOriValCount;  }
      else if (sensor == SensorManager.SENSOR_ACCELEROMETER)
      {
        ++mAccValCount;
      }
      setCount();
    }

    private void setCount()
    {
      StringBuffer buff = new StringBuffer();
      buff.append("mOriValCount:").append(mOriValCount).append("\n");
      buff.append("mOriAccCount:").append(mOriAccCount).append("\n");
      buff.append("mAccValCount:").append(mAccValCount).append("\n");
      buff.append("mAccAccCount:").append(mAccAccCount).append("\n");
      TextView tv = (TextViewfindViewById(R.id.counters);
      tv.setText(buff.toString());
    }
  };

  private SensorManager mManager;
  private boolean mSimulator;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    setupManager();
  }

  @Override
  protected void onResume()
  {
    if (mSimulator)
      SensorManagerSimulator.connectSimulator();
    listSensors();
    mManager.registerListener(mSensorListener, SensorManager.SENSOR_ORIENTATION | SensorManager.SENSOR_ACCELEROMETER);
    super.onResume();

  @Override
  protected void onStop()
  {
    mManager.unregisterListener(mSensorListener, SensorManager.SENSOR_ORIENTATION | SensorManager.SENSOR_ACCELEROMETER);
    if (mSimulator)
      SensorManagerSimulator.disconnectSimulator();
    super.onStop();
  }

  private void setupManager()
  {
    mManager = (SensorManagergetSystemService(SENSOR_SERVICE);
    if (mManager.getSensors() != 0)
    {
      // Real Device (Dev Phone)
      mSimulator = false;
      return;
    }
    // Emulator, Use OpenIntnet Simulator
    mSimulator = true;
    Hardware.mContentResolver = getContentResolver();
    mManager = new SensorManagerSimulator(mManager);
    startActivity(new Intent(Intent.ACTION_VIEW, Hardware.Preferences.CONTENT_URI));
    SensorManagerSimulator.connectSimulator();
  }

  private void listSensors()
  {
    Resources res = getResources();
    StringBuffer buff = new StringBuffer();
    int sensors = mManager.getSensors();
    buff.append(sensors);
    buff.append("\n");
    if ((sensors & SensorManager.SENSOR_ORIENTATION!= 0)
    {
      buff.append(res.getString(R.string.sensor_orientation));
      buff.append("\n");
    }
    if ((sensors & SensorManager.SENSOR_ORIENTATION_RAW!= 0)
    {
      buff.append(res.getString(R.string.sensor_orientation_raw));
      buff.append("\n");
    }
    if ((sensors & SensorManager.SENSOR_ACCELEROMETER!= 0)
    {
      buff.append(res.getString(R.string.sensor_accelerometer));
      buff.append("\n");
    }
    if ((sensors & SensorManager.SENSOR_TEMPERATURE!= 0)
    {
      buff.append(res.getString(R.string.sensor_temperature));
      buff.append("\n");
    }
    if ((sensors & SensorManager.SENSOR_MAGNETIC_FIELD!= 0)
    {
      buff.append(res.getString(R.string.sensor_magnetic_field));
      buff.append("\n");
    }
    if ((sensors & SensorManager.SENSOR_LIGHT!= 0)
    {
      buff.append(res.getString(R.string.sensor_light));
      buff.append("\n");
    }
    if ((sensors & SensorManager.SENSOR_PROXIMITY!= 0)
    {
      buff.append(res.getString(R.string.sensor_proximity));
      buff.append("\n");
    }
    if ((sensors & SensorManager.SENSOR_TRICORDER!= 0)
    {
      buff.append(res.getString(R.string.sensor_tricorder));
      buff.append("\n");

創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()