Android Open Source - android-dev-data-viewer Sensor Fragment






From Project

Back to project page android-dev-data-viewer.

License

The source code is released under:

MIT License

If you think the Android project android-dev-data-viewer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

// The MIT License (MIT)
////from www.j av a2 s .  co  m
// Copyright (c) 2014 mruddy
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package com.mruddy.devdataviewer;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class SensorFragment extends Fragment implements SensorEventListener, LocationListener {
  private TextView        accelerometerTextView;
  private TextView        gpsTextView;
  private SensorManager   sensorManager;
  private LocationManager locationManager;

  /**
   * @see android.hardware.SensorEventListener#onAccuracyChanged(android.hardware.Sensor, int)
   */
  @Override
  public void onAccuracyChanged(final Sensor sensor, final int accuracy) {
  }

  @Override
  public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
    return inflater.inflate(R.layout.sensor_fragment, container, false);
  }

  /**
   * @see android.location.LocationListener#onLocationChanged(android.location.Location)
   */
  @Override
  public void onLocationChanged(final Location location) {
    this.gpsTextView.setText("lat: " + location.getLatitude() + "\nlon: " + location.getLongitude());
  }

  /**
   * @see android.support.v4.app.Fragment#onPause()
   */
  @Override
  public void onPause() {
    super.onPause();
    this.sensorManager.unregisterListener(this);
    this.locationManager.removeUpdates(this);
  }

  /**
   * @see android.location.LocationListener#onProviderDisabled(java.lang.String)
   */
  @Override
  public void onProviderDisabled(final String provider) {
  }

  /**
   * @see android.location.LocationListener#onProviderEnabled(java.lang.String)
   */
  @Override
  public void onProviderEnabled(final String provider) {
  }

  /**
   * @see android.support.v4.app.Fragment#onResume()
   */
  @Override
  public void onResume() {
    super.onResume();
    this.accelerometerTextView = (TextView) this.getActivity().findViewById(R.id.textViewAccelerometer);
    this.gpsTextView = (TextView) this.getActivity().findViewById(R.id.textViewGPS);
    this.sensorManager = (SensorManager) this.getActivity().getSystemService(Context.SENSOR_SERVICE);
    final Sensor accelerometerSensor = this.sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    if (null != accelerometerSensor) {
      this.accelerometerTextView.setText("waiting for data...");
      this.sensorManager.registerListener(this, accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL);
    } else {
      this.accelerometerTextView.setText("accelerometer not found");
    }
    this.locationManager = (LocationManager) this.getActivity().getSystemService(Context.LOCATION_SERVICE);
    this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
  }

  /**
   * @see android.hardware.SensorEventListener#onSensorChanged(android.hardware.SensorEvent)
   */
  @Override
  public void onSensorChanged(final SensorEvent event) {
    if (Sensor.TYPE_ACCELEROMETER == event.sensor.getType()) {
      this.accelerometerTextView.setText(event.values[0] + "\n" + event.values[1] + "\n" + event.values[2]);
    }
  }

  /**
   * @see android.location.LocationListener#onStatusChanged(java.lang.String, int, android.os.Bundle)
   */
  @Override
  public void onStatusChanged(final String provider, final int status, final Bundle extras) {
  }
}




Java Source Code List

com.mruddy.devdataviewer.DevDataListFragment.java
com.mruddy.devdataviewer.MainActivity.java
com.mruddy.devdataviewer.SensorActivity.java
com.mruddy.devdataviewer.SensorFragment.java