Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ukgamer.gps;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import android.app.Activity;
- import android.app.ProgressDialog;
- 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.widget.TextView;
- public class main extends Activity {
- private LocationManager lm;
- private LocationListener locationListener;
- private SensorManager sm;
- private SensorEventListener sensorListener;
- private ProgressDialog pd;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- pd = ProgressDialog.show(this, getString(R.string.waitingtitle), getString(R.string.waitingtxt), true, false);
- lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
- locationListener = new MyLocationListener();
- sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
- sensorListener = new MySensorListener();
- lm.requestLocationUpdates(
- LocationManager.GPS_PROVIDER,
- 0,
- 0,
- locationListener);
- sm.registerListener(
- sensorListener,
- sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
- SensorManager.SENSOR_DELAY_UI);
- }
- @Override
- public void onPause() {
- lm.removeUpdates(locationListener);
- sm.unregisterListener(sensorListener);
- super.onPause();
- }
- @Override
- protected void onResume()
- {
- lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
- sm.registerListener(sensorListener, sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_UI);
- super.onResume();
- }
- private class MyLocationListener implements LocationListener
- {
- @Override
- public void onLocationChanged(Location loc) {
- if (loc == null) return;
- TextView txttime = (TextView)findViewById(R.id.txt_time);
- TextView txtsats = (TextView)findViewById(R.id.txt_sats);
- TextView txtlat = (TextView)findViewById(R.id.txt_lat);
- TextView txtlon = (TextView)findViewById(R.id.txt_lon);
- TextView txtalt = (TextView)findViewById(R.id.txt_alt);
- TextView txtbear = (TextView)findViewById(R.id.txt_bear);
- TextView txtspeed = (TextView)findViewById(R.id.txt_speed);
- TextView txtacc = (TextView)findViewById(R.id.txt_acc);
- if (pd.isShowing()) pd.dismiss();
- txttime.setText(new SimpleDateFormat("HH:mm:ss").format(new Date(loc.getTime())));
- txtsats.setText(R.string.waitingtitle);
- txtlat.setText(Location.convert(loc.getLatitude(), Location.FORMAT_MINUTES));
- txtlon.setText(Location.convert(loc.getLongitude(), Location.FORMAT_MINUTES));
- txtalt.setText(Double.toString(loc.getAltitude()) + getString(R.string.mMeter));
- txtacc.setText(Float.toString(loc.getAccuracy()) + getString(R.string.mMeter));
- if (loc.getSpeed() != 0.0f) {
- txtbear.setText(Float.toString(loc.getBearing()) + getString(R.string.mDeg));
- txtspeed.setText(Float.toString(loc.getSpeed()) + getString(R.string.mSpeed));
- } else {
- txtbear.setText(R.string.notmoving);
- txtspeed.setText(R.string.notmoving);
- }
- }
- @Override
- public void onProviderDisabled(String provider) { }
- @Override
- public void onProviderEnabled(String provider) { }
- @Override
- public void onStatusChanged(String provider, int status, Bundle extras) { }
- }
- private class MySensorListener implements SensorEventListener
- {
- @Override
- public void onAccuracyChanged(Sensor sensor, int accuracy) { }
- @Override
- public void onSensorChanged(SensorEvent event) {
- if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER) return;
- TextView txtaccel = (TextView)findViewById(R.id.txt_accel);
- txtaccel.setText(String.format("%f\n%f\n%f", event.values[0], event.values[1], event.values[2]));
- //txtmeterx.setText(Float.toString(event.values[0]));
- //txtmetery.setText(Float.toString(event.values[1]));
- //txtmeterz.setText(Float.toString(event.values[2]));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement