7
votes

Comment obtenir un emplacement GPS une fois pendant 5 minutes d'Android?

Bonjour, quelqu'un peut-il me donner un exemple de code pour obtenir un emplacement pour toutes les cinq minutes s'il vous plaît avoir essayé et je peux obtenir l'emplacement une fois en cliquant sur le bouton, Mais j'en ai besoin d'être affiché une fois pendant cinq minutes.

merci p>

Ceci est mon code: p>

public void checkLocation(View v) {

        //initialize location manager
        manager =  (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        //check if GPS is enabled
        //if not, notify user with a toast
        if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            Toast.makeText(this, "GPS is disabled.", Toast.LENGTH_SHORT).show();
        } else {

            //get a location provider from location manager
            //empty criteria searches through all providers and returns the best one
            String providerName = manager.getBestProvider(new Criteria(), true);
            Location location = manager.getLastKnownLocation(providerName);

            TextView tv = (TextView)findViewById(R.id.locationResults);
            if (location != null) {
                tv.setText(location.getLatitude() + " latitude, " + location.getLongitude() + " longitude");
            } else {
                tv.setText("Last known location not found. Waiting for updated location...");
            }
            //sign up to be notified of location updates every 15 seconds - for production code this should be at least a minute
            manager.requestLocationUpdates(providerName, 15000, 1, this);
        }
    }

    @Override
    public void onLocationChanged(Location location) {
        TextView tv = (TextView)findViewById(R.id.locationResults);
        if (location != null) {
            tv.setText(location.getLatitude() + " latitude, " + location.getLongitude() + " longitude");
        } else {
            tv.setText("Problem getting location");
        }
    }

    @Override
    public void onProviderDisabled(String arg0) {}

    @Override
    public void onProviderEnabled(String arg0) {}

    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {}

    // Find the closest Bart Station
    public String findClosestBart(Location loc) {
        double lat = loc.getLatitude();
        double lon = loc.getLongitude();

        double curStatLat = 0;
        double curStatLon = 0;
        double shortestDistSoFar = Double.POSITIVE_INFINITY;
        double curDist;
        String curStat = null;
        String closestStat = null;

        //sort through all the stations
        // write some sort of for loop using the API.

        curDist = Math.sqrt( ((lat - curStatLat) * (lat - curStatLat)) +
                        ((lon - curStatLon) * (lon - curStatLon)) );
        if (curDist < shortestDistSoFar) {
            closestStat = curStat;
        }

        return closestStat;

        }   


2 commentaires

Utilisez des fonctions de manutention et mettez à jour toutes les cinq minutes. le code qui est appuyez sur Mettre dans cette fonction


Comment utiliser une fonction de gestionnaire pouvez-vous modifier ce code pls


4 Réponses :


1
votes

J'ai utilisé coureur pour ce faire,

    final Runnable r = new Runnable() {
        public void run() {
    //Here add your code location listener call
    handler.postDelayed(this, 300000 );
        }
    };

    handler.postDelayed(r, 300000 );


0 commentaires

2
votes

Salut Utilisez le code de minuterie ci-dessous.

Vous pouvez utiliser les options ci-dessous Option 1 forte> Cela obtiendra les emplacements si Mobile déplacé 100 mètres. P>

   TimerTask refresher;
        // Initialization code in onCreate or similar:
        timer = new Timer();    
        refresher = new TimerTask() {
            public void run() {
              handler.sendEmptyMessage(0);
            };
        };
        // first event immediately,  following after 1 seconds each
        timer.scheduleAtFixedRate(refresher, 0,1000); 
        //=======================================================


final Handler handler = new Handler() {


        public void handleMessage(Message msg) {
              switch (msg.what) {
              case REFRESH: 
                  //your code here 

                  break;
              default:
                  break;
              }
          }
        };


0 commentaires

0
votes

Essayez comme ceci: xxx


0 commentaires

13
votes

Voici le code permettant d'obtenir l'emplacement et de définir l'écoute de GPS pour obtenir l'emplacement actuel sur quelques minutes et une distance, j'ai également utilisé un objet runnable pour obtenir l'emplacement toutes les quelques minutes.

private LocationListener GPSListener = new LocationListener(){
    public void onLocationChanged(Location location) {
        // update location
        locMan.removeUpdates(GPSListener); // remove this listener
    }

    public void onProviderDisabled(String provider) {
    }

    public void onProviderEnabled(String provider) {
    }

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


1 commentaires

Le suivi de l'emplacement va s'arrêter lorsque l'utilisateur ferme l'application. Comment pouvons-nous faire cela avec un service?