package com.mis.ARTestApp;
import java.util.Iterator;
import java.util.List;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.widget.TextView;
public class ARTestAppSensorActivity
extends Activity
implements SensorEventListener
{
private SensorManager mSensorManager
;
private Sensor mAccelerometer
;
private Sensor mCompass
;
private List<Sensor
> mSensorList
;
private LocationManager mLocationManager
;
private String mLocationProviderName = LocationManager.
NETWORK_PROVIDER;
/** Called when the activity is first created. */
@
Override
public void onCreate
(Bundle savedInstanceState
) {
super.
onCreate(savedInstanceState
);
setContentView
(R.
layout.
sensor);
mSensorManager =
(SensorManager
)getSystemService
(SENSOR_SERVICE
);
mSensorList = mSensorManager.
getSensorList(Sensor.
TYPE_ALL);
StringBuilder sensors =
new StringBuilder();
for (Iterator<Sensor
> i = mSensorList.
iterator(); i.
hasNext();) {
Sensor s = i.
next();
sensors.
append(s.
getName() +
"\n");
}
TextView sensorListView =
(TextView
)findViewById
(R.
id.
sensorlist);
sensorListView.
setText(sensors.
toString());
mAccelerometer = mSensorManager.
getDefaultSensor(Sensor.
TYPE_ACCELEROMETER);
mCompass = mSensorManager.
getDefaultSensor(Sensor.
TYPE_MAGNETIC_FIELD);
mLocationManager =
(LocationManager
)getSystemService
(LOCATION_SERVICE
);
Criteria c =
new Criteria
();
c.
setAccuracy(Criteria.
ACCURACY_FINE);
mLocationProviderName = mLocationManager.
getBestProvider(c,
true);
mLocationManager.
requestLocationUpdates(mLocationProviderName,
0,
0, mLocationListener
);
Location lastKnownLocation = mLocationManager.
getLastKnownLocation(mLocationProviderName
);
TextView gpsTextView =
(TextView
)findViewById
(R.
id.
gpstext);
gpsTextView.
setText(("Location: Lat " + lastKnownLocation.
getLatitude() +
" Long " + lastKnownLocation.
getLongitude()));
}
protected void onResume
() {
super.
onResume();
mSensorManager.
registerListener(this, mAccelerometer, SensorManager.
SENSOR_DELAY_NORMAL);
mSensorManager.
registerListener(this, mCompass, SensorManager.
SENSOR_DELAY_NORMAL);
mLocationManager.
requestLocationUpdates(mLocationProviderName,
0,
0, mLocationListener
);
}
protected void onPause
() {
super.
onPause();
mSensorManager.
unregisterListener(this);
mLocationManager.
removeUpdates(mLocationListener
);
}
LocationListener mLocationListener =
new LocationListener
() {
public void onLocationChanged
(Location location
) {
// Called when a new location is found by the network location provider.
TextView gpsTextView =
(TextView
)findViewById
(R.
id.
gpstext);
gpsTextView.
setText(("Location: Lat " + location.
getLatitude() +
" Long " + location.
getLongitude()));
}
public void onStatusChanged
(String provider,
int status, Bundle extras
) {}
public void onProviderEnabled
(String provider
) {}
public void onProviderDisabled
(String provider
) {}
};
public void onAccuracyChanged
(Sensor sensor,
int accuracy
) {
}
public void onSensorChanged
(SensorEvent event
) {
if (event.
sensor.
getType() == Sensor.
TYPE_ACCELEROMETER) {
TextView gsensorTextView =
(TextView
)findViewById
(R.
id.
gsensortext);
gsensorTextView.
setText("Accelerometer: X: " + event.
values[0] +
" Y:" + event.
values[1] +
" Z:" + event.
values[2]);
}
else if (event.
sensor.
getType() == Sensor.
TYPE_MAGNETIC_FIELD) {
TextView compassTextView =
(TextView
)findViewById
(R.
id.
compasstext);
compassTextView.
setText("Compass: X: " + event.
values[0] +
" Y:" + event.
values[1] +
" Z:" + event.
values[2]);
}
}
}