Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.contakm;
- import android.app.Activity;
- import android.content.Context;
- import android.location.GpsStatus;
- import android.location.GpsStatus.Listener;
- import android.location.Location;
- import android.location.LocationListener;
- import android.location.LocationManager;
- import android.os.Bundle;
- import android.util.Log;
- import android.widget.TextView;
- public class AndroidGPSTrackingActivity extends Activity {
- private LocationManager lm;
- private TextView speed,longitude,latitude,averagespeed;
- private double averageS;
- int count;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- speed=(TextView)findViewById(R.id.textView1);
- longitude=(TextView)findViewById(R.id.textView5);
- latitude=(TextView)findViewById(R.id.textView4);
- averagespeed=(TextView)findViewById(R.id.textView7);
- longitude.setText("");
- latitude.setText("");
- // avvio il servizio
- lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
- // creo il provider gps
- if ( lm.isProviderEnabled(LocationManager.GPS_PROVIDER) ) {
- startGpsTracking();
- }else{
- //se vi sono errori
- }
- }
- //questo metodo, non fa altro che avviare la connessione
- private void startGpsTracking() {
- lm.addGpsStatusListener(gpsListener);
- //precisione
- lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000 , 5, myLocListener );
- }
- //listener per le azioni
- private Listener gpsListener= new Listener () {
- public void onGpsStatusChanged(int status) {
- switch (status) {
- case GpsStatus.GPS_EVENT_FIRST_FIX:
- Log.d("tag", "onGpsStatusChanged First Fix");
- break;
- case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
- Log.d("tag", "onGpsStatusChanged Satellite");
- break;
- case GpsStatus.GPS_EVENT_STARTED:
- Log.d("tag", "onGpsStatusChanged Started");
- break;
- case GpsStatus.GPS_EVENT_STOPPED:
- Log.d("tag", "onGpsStatusChanged Stopped");
- break;
- }
- }
- };
- //listener per la locazione
- private LocationListener myLocListener = new LocationListener(){
- public void onLocationChanged(Location location) {
- Log.d("tag", "onLocationChanged");
- speed.setText(((int)(location.getSpeed()*3.6))+"km/h");
- longitude.setText(location.getLongitude()+"");
- latitude.setText(location.getLatitude()+"");
- count++;
- averageS+=location.getSpeed()*3.6;
- averageS/=count;
- averagespeed.setText(((int)averageS)+"km/h");
- }
- public void onProviderDisabled(String provider) {
- Log.d("tag", "onProviderDisabled");
- }
- public void onProviderEnabled(String provider) {
- Log.d("tag", "onProviderEnabled");
- }
- public void onStatusChanged(String provider, int status, Bundle extras) {
- Log.d("tag", "onStatusChanged");
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement