Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.34 KB | None | 0 0
  1. package ukgamer.gps;
  2.  
  3.  
  4. import java.text.SimpleDateFormat;
  5. import java.util.Date;
  6.  
  7. import android.app.Activity;
  8. import android.app.ProgressDialog;
  9. import android.content.Context;
  10. import android.hardware.Sensor;
  11. import android.hardware.SensorEvent;
  12. import android.hardware.SensorEventListener;
  13. import android.hardware.SensorManager;
  14. import android.location.Location;
  15. import android.location.LocationListener;
  16. import android.location.LocationManager;
  17. import android.os.Bundle;
  18. import android.widget.TextView;
  19.  
  20. public class main extends Activity {
  21.     private LocationManager lm;
  22.     private LocationListener locationListener;
  23.     private SensorManager sm;
  24.     private SensorEventListener sensorListener;
  25.    
  26.     private ProgressDialog pd;
  27.    
  28.     /** Called when the activity is first created. */
  29.     @Override
  30.     public void onCreate(Bundle savedInstanceState) {
  31.         super.onCreate(savedInstanceState);
  32.         setContentView(R.layout.main);
  33.        
  34.         pd = ProgressDialog.show(this, getString(R.string.waitingtitle), getString(R.string.waitingtxt), true, false);
  35.        
  36.         lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);    
  37.         locationListener = new MyLocationListener();
  38.        
  39.         sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
  40.         sensorListener = new MySensorListener();
  41.  
  42.         lm.requestLocationUpdates(
  43.                 LocationManager.GPS_PROVIDER,
  44.                 0,
  45.                 0,
  46.                 locationListener);
  47.        
  48.         sm.registerListener(
  49.                 sensorListener,
  50.                 sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
  51.                 SensorManager.SENSOR_DELAY_UI);
  52.     }
  53.    
  54.     @Override
  55.     public void onPause() {
  56.         lm.removeUpdates(locationListener);
  57.         sm.unregisterListener(sensorListener);
  58.         super.onPause();
  59.     }
  60.    
  61.     @Override
  62.     protected void onResume()
  63.     {
  64.         lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
  65.         sm.registerListener(sensorListener, sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_UI);
  66.  
  67.         super.onResume();
  68.     }
  69.    
  70.     private class MyLocationListener implements LocationListener
  71.     {
  72.         @Override
  73.         public void onLocationChanged(Location loc) {
  74.        
  75.             if (loc == null) return;
  76.            
  77.             TextView txttime = (TextView)findViewById(R.id.txt_time);
  78.             TextView txtsats = (TextView)findViewById(R.id.txt_sats);
  79.             TextView txtlat = (TextView)findViewById(R.id.txt_lat);
  80.             TextView txtlon = (TextView)findViewById(R.id.txt_lon);
  81.             TextView txtalt = (TextView)findViewById(R.id.txt_alt);
  82.             TextView txtbear = (TextView)findViewById(R.id.txt_bear);
  83.             TextView txtspeed = (TextView)findViewById(R.id.txt_speed);
  84.             TextView txtacc = (TextView)findViewById(R.id.txt_acc);
  85.            
  86.             if (pd.isShowing()) pd.dismiss();
  87.  
  88.             txttime.setText(new SimpleDateFormat("HH:mm:ss").format(new Date(loc.getTime())));
  89.             txtsats.setText(R.string.waitingtitle);
  90.             txtlat.setText(Location.convert(loc.getLatitude(), Location.FORMAT_MINUTES));
  91.             txtlon.setText(Location.convert(loc.getLongitude(), Location.FORMAT_MINUTES));
  92.             txtalt.setText(Double.toString(loc.getAltitude()) + getString(R.string.mMeter));
  93.             txtacc.setText(Float.toString(loc.getAccuracy()) + getString(R.string.mMeter));
  94.            
  95.             if (loc.getSpeed() != 0.0f) {
  96.                 txtbear.setText(Float.toString(loc.getBearing()) + getString(R.string.mDeg));
  97.                 txtspeed.setText(Float.toString(loc.getSpeed()) + getString(R.string.mSpeed));
  98.             } else {
  99.                 txtbear.setText(R.string.notmoving);
  100.                 txtspeed.setText(R.string.notmoving);
  101.             }
  102.     }
  103.  
  104.         @Override
  105.         public void onProviderDisabled(String provider) { }
  106.  
  107.         @Override
  108.         public void onProviderEnabled(String provider) { }
  109.  
  110.         @Override
  111.         public void onStatusChanged(String provider, int status, Bundle extras) { }
  112.     }
  113.    
  114.     private class MySensorListener implements SensorEventListener
  115.     {
  116.         @Override
  117.         public void onAccuracyChanged(Sensor sensor, int accuracy) { }
  118.  
  119.         @Override
  120.         public void onSensorChanged(SensorEvent event) {
  121.             if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER) return;
  122.            
  123.             TextView txtaccel = (TextView)findViewById(R.id.txt_accel);
  124.             txtaccel.setText(String.format("%f\n%f\n%f", event.values[0], event.values[1], event.values[2]));
  125.            
  126.             //txtmeterx.setText(Float.toString(event.values[0]));
  127.             //txtmetery.setText(Float.toString(event.values[1]));
  128.             //txtmeterz.setText(Float.toString(event.values[2]));
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement