0

在我的应用程序中,我想让用户有可能了解他的本地化...但我想同时实现 GPS 和网络提供程序...因为 GPS 提供程序并不总是可用...我使用模拟器和我不知道我的代码是否有效...

这是我的代码源:(提前致谢)

 locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
            LocationListener mlocListener = new MyLocationListener();
            locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
           locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, mlocListener);

            Criteria criteria = new Criteria();
            provider = locationManager.getBestProvider(criteria, false);
            location = locationManager.getLastKnownLocation(provider);

            if(location!=null)
            {
                mylat=location.getLatitude();
                mylong=location.getLongitude();
            }

这是 myLocationlistener 类:

public class MyLocationListener implements LocationListener

        {

        @Override

        public void onLocationChanged(Location loc)

        {

            Log.i("location", "lat :"+loc.getLatitude());
            mylat=loc.getLatitude();
            mylong=loc.getLongitude();

            for(int i=0; i<results.size(); i++)
            {


                gare g=results.get(i);


            }

            list.invalidateViews();


        }


        @Override

        public void onProviderDisabled(String provider)

        {



        AlertDialog alertDialog = new AlertDialog.Builder(liste_activity.this).create();
        alertDialog.setTitle("Le GPS est désactivé");
        alertDialog.setMessage(" activez le GPS");
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int which) {


        } }); 
        alertDialog.show();


        }


        @Override

        public void onProviderEnabled(String provider)

        {



        }


        @Override

        public void onStatusChanged(String provider, int status, Bundle extras)

        {


        }
    }
4

1 回答 1

0

对我来说看上去很好。我通常将我的#requestLocationUpdates() 包装在首先检查可用性的条件中。

if (this.mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
    this.mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}

if (this.mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
    this.mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
}
于 2013-01-17T18:35:08.503 回答