Domanda

private class ExecuteLocations extends AsyncTask<String, Void, Void>{
    private final ProgressDialog dialog = new ProgressDialog(ListProfiles.this);

    protected void onPreExecute() {
        //this.dialog.setMessage("Starting pre-execute...");
        //this.dialog.show();
    }

    @Override
    protected Void doInBackground(String... arg0) {
        check_profiles_lm=(LocationManager) ListProfiles.this.getSystemService(LOCATION_SERVICE);  
        myLocListen = new LocationListener(){
            @Override
            public void onLocationChanged(Location location) {
                HashMap params = new HashMap();
                params.put("lat", Double.toString(location.getLatitude()));
                params.put("long", Double.toString(location.getLongitude()));
                postData("http://mydomain.com",params);

            }
            @Override
            public void onStatusChanged(String provider, int status,Bundle extras) { 
            }
            @Override
            public void onProviderDisabled(String provider) { 
            }
            @Override
            public void onProviderEnabled(String provider) {
            }
        };      
        check_profiles_lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 30000, 0, myLocListen);
        return null;
    }

    protected void  onPostExecute(final Void unused) {
        if (this.dialog.isShowing()) {
            //this.dialog.dismiss();
        }
        //Do something else here 
    }

}

In sostanza, il mio obiettivo è:

  1. costantemente inviare la latitudine / longitudine al mio sito web ogni minuto o giù di lì.
  2. Certo, io voglio fare questo in un altro thread, in modo da non rovinare il mio thread dell'interfaccia utente. Questo AsyncTask dovrebbe risolvere questo ... ma è allevare un errore e io non so perché.

Cosa si può fare per raggiungere il mio obiettivo? E 'molto semplice ... basta posizione di scansione e post al web ogni minuto, in background.

A proposito, il mio metodo onCreate è così. Questo è fondamentalmente.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
            new ExecuteLocations().execute();
            setContentView(R.layout.main_list);
    }
È stato utile?

Soluzione

Break it in 3 fasi:

  1. make quello che vuoi lavorare lavoro w / o AsyncTask
  2. Assicurati di aver capito AsyncTask, fare un semplice Log.e ( "haha", "Beccato") nel doInBackground(...) e verificare che mostra
  3. Combinare le due cose.

Per questo caso, probabilmente sarei andare con un Service innescato da AlarmManager. Immagino che avrete bisogno di queste ultime in ogni modo.

Altri suggerimenti

Crea questo come un servizio. Non c'è bisogno di avere un timer o AlarmManager come si registra per gli eventi posizione e agire in modo appropriato.

Un esempio di ascolto di eventi di localizzazione può essere trovato alla http : //code.google.com/p/android-bluetooth-on-motion/source/browse/#svn/trunk/BluetoothOnMotion/src/com/elsewhat

        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_NETWORK, MIN_DISTANCE_NETWORK,locationListener);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_GPS, MIN_DISTANCE_GPS,locationListener);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top