在Android中获取Accelerometer值

时间:2014-09-30 05:26:04

标签: android

我正在尝试将加速度计X,Y,Z值存储在文本文件中,但是我得到了运行时错误,在几秒钟后类似的值开始插入文件中。

简单的GUI enter image description here

Android Apps

显示文本文件值的TextArea。

现在代码在这里,请问有人可以告诉我我在哪里做错了或者我应该在代码中做些什么改变? 感谢


public class MainActivity extends Activity implements SensorEventListener 
{

private SensorManager mSensorManager;
private Sensor mAccelerometer;
TextView title,tvx,tvy,tvz;
EditText etshowval;
RelativeLayout layout;
private String acc;
private String read_str = "";
private final String filepath = "/sdcard/acc.txt";
private BufferedWriter mBufferedWriter;
private BufferedReader mBufferedReader;
private float x;
private float y;
private float z;

public static final int MSG_DONE = 1;
public static final int MSG_ERROR = 2;
public static final int MSG_STOP = 3;

private boolean mrunning;
private Handler mHandler;
private HandlerThread mHandlerThread;

private Handler uiHandler = new Handler(){
    public void handleMessage(Message msg){ 
        String str = (String) msg.obj;
        switch (msg.what)
        {
            case MSG_DONE:
                Toast.makeText(getBaseContext(), str,
                        Toast.LENGTH_SHORT).show();
                break;
            case MSG_ERROR: 
                Toast.makeText(getBaseContext(),str,
                        Toast.LENGTH_SHORT).show();
                break;
            case MSG_STOP:
                Toast.makeText(getBaseContext(), str,
                        Toast.LENGTH_SHORT).show();
            default:
                break;
        }
        super.handleMessage(msg);
    }
};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mSensorManager.registerListener(this, mAccelerometer,SensorManager.SENSOR_DELAY_NORMAL);
    //get layout
    layout = (RelativeLayout) findViewById(R.id.relative);

    //get textviews
    title = (TextView)findViewById(R.id.name);   
    tvx = (TextView)findViewById(R.id.xval);
    tvy = (TextView)findViewById(R.id.yval);
    tvz = (TextView)findViewById(R.id.zval);
    etshowval = (EditText)findViewById(R.id.showval);
    title.setText("Accelerator");

    mHandlerThread = new HandlerThread("Working Thread");
    mHandlerThread.start();

    mHandler = new Handler(mHandlerThread.getLooper());
    mHandler.post(r);
}

private Runnable r = new Runnable(){
    @Override
    public void run ()
    {
        while(true)
        {
            if (mrunning)
            {
                Message msg1 = new Message();
                try 
                {
                    WriteFile(filepath,acc);
                    msg1.what = MSG_DONE;
                    msg1.obj = "Start to write to SD 'acc.txt'";
                } 
                catch (Exception e) 
                {
                    msg1.what = MSG_ERROR;
                    msg1.obj = e.getMessage();
                }
                uiHandler.sendMessage(msg1);
            }
            else
            {   
                Message msg2 = new Message();
                msg2.what = MSG_STOP;
                msg2.obj = "Stop to write to SD 'acc.txt'";
                uiHandler.sendMessage(msg2);
            }

            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
};

public void onStartClick(View view) 
{
    start();
}

public void onStopClick(View view) 
{
    stop();
}

public void onReadClick(View view) 
{
    etshowval.setText(ReadFile(filepath));
}

private synchronized void start()
{
    mrunning = true;
}

private synchronized void stop()
{
    mrunning = false;
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) 
{
}

@Override
public void onSensorChanged(SensorEvent sensorEvent) 
{
    // TODO Auto-generated method stub

    if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) 
    {
        x = sensorEvent.values[0]; 
        y = sensorEvent.values[1]; 
        z = sensorEvent.values[2]; 
        acc= String.valueOf(x) + ", " + String.valueOf(y) + ", " + String.valueOf(z);

        tvx.setText("X = "+ String.valueOf(x));
        tvy.setText("Y = "+ String.valueOf(y));
        tvz.setText("Z = "+ String.valueOf(z));
    }
  }

  public void CreateFile(String path)
  {
    File f = new File(path);
    try {
        Log.d("ACTIVITY", "Create a File.");
        f.createNewFile();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
  }

  public String ReadFile (String filepath)
  {
    mBufferedReader = null;
    String tmp = null;

    if (!FileIsExist(filepath))
        CreateFile(filepath);

    try 
    {
        mBufferedReader = new BufferedReader(new FileReader(filepath));
        // Read string
        while ((tmp = mBufferedReader.readLine()) != null) 
        {
            tmp += "\n";
            read_str += tmp;
        }
    } 
    catch (IOException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return read_str;
  }

   public void WriteFile(String filepath, String str)
   {
    mBufferedWriter = null;

    if (!FileIsExist(filepath))
        CreateFile(filepath);

    try 
    {
        mBufferedWriter = new BufferedWriter(new FileWriter(filepath, true));
        mBufferedWriter.write(str);
        mBufferedWriter.newLine();
        mBufferedWriter.flush();
        mBufferedWriter.close();
    }
    catch (IOException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
   }

   public boolean FileIsExist(String filepath)
   {
    File f = new File(filepath);

    if (! f.exists())
    {
        Log.e("ACTIVITY", "File does not exist.");
        return false;
    }
    else
        return true;
   }

    @Override
   protected void onPause()
   {
   // TODO Auto-generated method stub
    mSensorManager.unregisterListener(this);
    Toast.makeText(this, "Unregister accelerometerListener", Toast.LENGTH_LONG).show();
    super.onPause();
   } 
  }

0 个答案:

没有答案