質問

In my application I want to give the possibility to the user to know his localisation...but I want to implement both a GPS and Network provider ...because the GPS provider is not always available...I use the Emulator and I don't know if my code works or not ...

This is my code source: (thanks in advance)

 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();
            }

And this is the class 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)

        {


        }
    }
役に立ちましたか?

解決

Looks good to me. I usually wrap my #requestLocationUpdates() in conditionals that check for availability first.

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);
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top