无信号  
package com.xmb.MyGps2;

import android.app.Activity;
import android.util.Log;
import android.os.Bundle;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.GpsSatellite;
import android.widget.TextView;
import android.content.Context;
import java.util.Iterator;

public class MyGps2 extends Activity {
    LocationManager mLocationManager;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        String provider = mLocationManager.GPS_PROVIDER;
        Location location = mLocationManager.getLastKnownLocation(provider);
        mLocationManager.requestLocationUpdates(provider, 4000, 10,
                locationListener);
        mLocationManager.addGpsStatusListener(statusListener);
        updateWithNewLocation(location);
        updateGpsStatus(4, null);
    }

    public void onDestroy() {
        super.onDestroy();
    }

    private final GpsStatus.Listener statusListener = new GpsStatus.Listener() {
        public void onGpsStatusChanged(int event) {
            Log.w("out", "now gps status changed" + event);
            GpsStatus status = mLocationManager.getGpsStatus(null);
            updateGpsStatus(event, status);
        }
    };

    private final LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            Log.w("out", "now location changed");
            updateWithNewLocation(location);
        }

        public void onProviderDisabled(String provider) {
            Log.w("out", "now provider disable");
            updateWithNewLocation(null);
        }

        public void onProviderEnabled(String provider) {
            Log.w("out", "now provider enable");
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
            Log.w("out", "now provider status changed" + status);
        }
    };

    private void updateGpsStatus(int event, GpsStatus status) {
        TextView slView = (TextView) findViewById(R.id.tv1);
        if (status == null) {
            slView.setText("Satellites:" + "0");
        } else if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) {
            int maxSatellites = status.getMaxSatellites();
            Iterator<GpsSatellite> it = status.getSatellites().iterator();
            int count = 0;
            while (it.hasNext() && count <= maxSatellites) {
                GpsSatellite s = it.next();
                count++;
            }
            slView.setText("Satellites:" + count);
        }
    }

    private void updateWithNewLocation(Location location) {
        if (location != null) {
            double lat = location.getLatitude();
            double lng = location.getLongitude();
            TextView latView = (TextView) findViewById(R.id.tv1);
            TextView lngView = (TextView) findViewById(R.id.tv2);
            latView.setText("longitude"
                    + String.format("%.5f", lat));
            lngView.setText("latitude"
                    + String.format("%.5f", lng));
        } else {
            TextView latView = (TextView) findViewById(R.id.tv1);
            TextView lngView = (TextView) findViewById(R.id.tv2);
            latView.setText("cannot get geo info");
            lngView.setText("");
        }
    }
}

 

posted on 2013-04-10 15:54  BenXian  阅读(809)  评论(0编辑  收藏  举报