miércoles, 26 de septiembre de 2012

Android: GPS Test (Android 2.2) funciona xD

package com.example.gpstest2;

//import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements LocationListener {

    private LocationManager locationManager;
    TextView t;     
    //private String provider;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.setTitle("test gps rck");  
        
        //telnet localhost 5554
        //geo fix 30 26
        
        // Get the location manager
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        
        t = (TextView) findViewById(R.id.tit);    
        t.setText("inicio..");
        
        //LocationListener mlocListener = new LocationListener();
        //locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
        //locationManager.requestLocationUpdates(provider, 400, 1, this);
        
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 400, 1, this);
        
        boolean enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        // Check if enabled and if not send user to the GSP settings
        // Better solution would be to display a dialog and suggesting to
        // go to the settings
        if (!enabled) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);
            Toast.makeText(getBaseContext(), "gps no esta disponible", Toast.LENGTH_LONG).show();
        }
        
        Toast.makeText(getBaseContext(), "GPS disponible", Toast.LENGTH_LONG).show();

        /*
        // Define the criteria how to select the locatioin provider -> use
        // default
        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, false);
        Location location = locationManager.getLastKnownLocation(provider);

        // Initialize the location fields
        if (location != null) {
          System.out.println("Provider " + provider + " has been selected.");
          onLocationChanged(location);
        } else {
          //latituteField.setText("Location not available");
          //longitudeField.setText("Location not available");
            Toast.makeText(getBaseContext(), "Location not available", Toast.LENGTH_LONG).show();
        }
        */
    }

    
    @Override
    protected void onResume() {
      super.onResume();
      //locationManager.requestLocationUpdates(provider, 400, 1, this);
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 400, 1, this);
    }

    
    @Override
    protected void onPause() {
      super.onPause();
      locationManager.removeUpdates(this);
    }

    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public void onLocationChanged(Location loc) {
        // TODO Auto-generated method stub
        //loc.getLatitude();
        //loc.getLongitude();
        
        String Text = "My current location is: Latitud = " + loc.getLatitude() + " Longitud = " + loc.getLongitude();

        Toast.makeText(getBaseContext(), Text, Toast.LENGTH_LONG).show();
        
            
        t.setText("cambio: " + Text);

    }

    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
        Toast.makeText( getBaseContext(),"Gps Disabled",Toast.LENGTH_SHORT ).show();
        
    }

    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
        Toast.makeText( getBaseContext(),"Gps Enabled",Toast.LENGTH_SHORT ).show();
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub
        
    }
}

No hay comentarios:

Publicar un comentario