android定位应用程序优化

android定位应用程序优化,android,networking,optimization,gps,switch-statement,Android,Networking,Optimization,Gps,Switch Statement,我已经创建了一个位置应用程序,它将在我的设备上的谷歌地图api中显示当前位置,但我对使用网络提供商和gps提供商感到困惑 我希望我的应用程序在打开应用程序时使用网络提供商,这样它就可以快速指出位置。然后它应该搜索gps提供商,一旦gps可用,它应该使用gps提供商。在运行应用程序的过程中,如果我失去了gps连接,它应该返回网络提供商,等待gps可用 我的源代码是 public class MyGoogleMap1Activity extends MapActivity { priva

我已经创建了一个位置应用程序,它将在我的设备上的谷歌地图api中显示当前位置,但我对使用网络提供商和gps提供商感到困惑

我希望我的应用程序在打开应用程序时使用网络提供商,这样它就可以快速指出位置。然后它应该搜索gps提供商,一旦gps可用,它应该使用gps提供商。在运行应用程序的过程中,如果我失去了gps连接,它应该返回网络提供商,等待gps可用

我的源代码是

public class MyGoogleMap1Activity extends MapActivity  
{
    private static final long min_distance = 1; // in Meters
    private static final long min_time = 1000; // in Milliseconds
    protected LocationManager locationManager;
    protected MyLocationListener locationListener;
    HelloItemizedOverlay itemizedoverlay;
    List<Overlay> mapOverlays;
    MapView mapView;

    /** Called when the activity is first created. */
    @Override public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        try
        {
            mapView = (MapView) findViewById(R.id.mapview);
            mapView.setBuiltInZoomControls(true);
            mapOverlays = mapView.getOverlays();
            Drawable drawable = this.getResources().getDrawable(R.drawable.google_maps_pin);
            itemizedoverlay = new HelloItemizedOverlay(drawable);
            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            locationListener = new MyLocationListener();
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
            min_time,min_distance ,locationListener);
        }
        catch(Exception e)
        {
            Toast.makeText(MyGoogleMap1Activity.this, e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }

    @Override protected boolean isRouteDisplayed() 
    {
        return false;
    }

    private class MyLocationListener implements LocationListener 
    {
        public void onLocationChanged(Location location) 
        {
            try
            {
                if (location != null)
                {
                    int lat = (int) ( location.getLatitude() * 1E6); //coordinates are in microdegrees
                    int  lng = (int) ( location.getLongitude() * 1E6);
                    GeoPoint point = new GeoPoint( lat,  lng);
                    OverlayItem overlayitem = new OverlayItem(point, "", "");
                    itemizedoverlay.addOverlay(overlayitem);
                    mapOverlays.add(itemizedoverlay);
                    MapController  myMapController = mapView.getController();
                    myMapController.animateTo(point);
                    myMapController.setZoom(16);
                }
                String message = String.format("Current Location \n Longitude: %1$s \n Latitude: 2$s",location.getLongitude(), location.getLatitude());
                Toast.makeText(MyGoogleMap1Activity.this,message, Toast.LENGTH_LONG).show();
            }
            catch(Exception e)
            {
                Toast.makeText(MyGoogleMap1Activity.this, e.getMessage(), Toast.LENGTH_LONG).show();
            }
        }

        public void onStatusChanged(String provider, int status, Bundle extras) 
        {
            Toast.makeText(MyGoogleMap1Activity.this,"Status Changed",Toast.LENGTH_LONG).show();
        }

        public void onProviderDisabled(String s) 
        {
            Toast.makeText(MyGoogleMap1Activity.this,"Provider disabled by the user. GPS turned off",Toast.LENGTH_LONG).show();
        }

        public void onProviderEnabled(String s) 
        {
            Toast.makeText(MyGoogleMap1Activity.this,"Provider enabled by the user. GPS turned     on",Toast.LENGTH_LONG).show();
        }

    }

}
下面是一个关于如何在不同的位置提供者之间切换的示例

请记住,网络供应商是非常不准确的。它依赖于手机站点信息。与10至50米的GPS精度相比,网络供应商的精度为100至3000米。
GPS提供商在建筑物或茂密森林/城市地区的卫星能见度受损。然而,自从引入辅助GPS以来,您不必依赖卫星的可见性。GPS提供商永远是更好的选择

不,我希望它一直处于活动状态,因此首先它应该捕获网络提供商,直到它激活gps。请帮我写代码