Back to project page TrafficTracker.
The source code is released under:
MIT License
If you think the Android project TrafficTracker listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.dev.nuwan.roadtrafficmonitor; /* www . ja v a2 s . c o m*/ import com.dev.nuwan.roadtrafficmonitor.LinearAccelaratorSensorListener.CalibratorListener; import android.support.v7.app.ActionBarActivity; import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorManager; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends ActionBarActivity { private SensorManager mSensorManager; private Sensor mSensor; private LinearAccelaratorSensorListener sEventListener = new LinearAccelaratorSensorListener(); private CalibratorListener cListener = new CalibratorListener(); /** * * Fire the calibrator */ private void fireCalibrator() { // TODO: Add a fancy screen mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); mSensor = mSensorManager .getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION); mSensorManager.registerListener(cListener, mSensor, SensorManager.SENSOR_DELAY_NORMAL); } /** * Initialize the linear acceleration sensor. */ private void fireSensor(View v) { mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); mSensor = mSensorManager .getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION); // mSensorManager.registerListener(cListener, mSensor, // SensorManager.SENSOR_DELAY_NORMAL); mSensorManager.unregisterListener(cListener); mSensorManager.registerListener(sEventListener, mSensor, SensorManager.SENSOR_DELAY_NORMAL); sEventListener.addUI((TextView)v); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // fireSensor(); final Button calibrateButton = (Button) findViewById(R.id.calibrateBtn); final TextView velocityTxt = (TextView) findViewById(R.id.velocityText); final Button startButton = (Button) findViewById(R.id.startBtn); calibrateButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { fireCalibrator(); } }); startButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // pass text to print velocity values fireSensor(velocityTxt); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override protected void onPause() { super.onPause(); mSensorManager.unregisterListener(sEventListener); } }