CERCA
PER MODELLO
FullScreen Chatbox! :)

Utente del giorno: megthebest con ben 1 Thanks ricevuti nelle ultime 24 ore
Utente della settimana: carotix con ben 4 Thanks ricevuti negli ultimi sette giorni
Utente del mese: megthebest con ben 21 Thanks ricevuti nell'ultimo mese

Visualizzazione dei risultati da 1 a 1 su 1
Discussione:

Servizio per memorizzare oggetti Location

Se questa discussione ti è stata utile, ti preghiamo di lasciare un messaggio di feedback in modo che possa essere preziosa in futuro anche per altri utenti come te!
  1. #1
    Baby Droid


    Registrato dal
    Sep 2011
    Messaggi
    6

    Ringraziamenti
    0
    Ringraziato 0 volte in 0 Posts
    Predefinito

    Servizio per memorizzare oggetti Location

    Salve a tutti, sto creando un servizio che automaticamente mi salvi la posizione gps ogni minuto. Faccio partire quindi il servizio da un activity e utilizzo la classe TimerTask per temporizzare l'operazione. All'interno del metodo run() di TimerTask inserisco i metodi per salvare la posizione ma in runtime mi da problemi (l'applicazione crasha e il logcat mi dice che il problema è nella implementazione del metodo run()). Se non utilizzo la temporizzazione, quindi salvo solo una volta la posizione gps tutto funziona.
    Vi posto anche il codice se qualcuno può dirmi dove sbaglio.. Grazie!!

    codice:
     
    1
    package it.helloworld.tour;
    2
    3
    import java.io.FileNotFoundException;
    4
    import java.io.IOException;
    5
    import java.io.ObjectOutputStream;
    6
    import android.app.Service;
    7
    import android.content.Context;
    8
    import android.content.Intent;
    9
    import android.location.Location;
    10
    import android.location.LocationListener;
    11
    import android.location.LocationManager;
    12
    import android.os.Bundle;
    13
    import android.os.IBinder;
    14
    import java.util.ArrayList;
    15
    import java.util.Timer; 
    16
    import java.util.TimerTask; 
    17
    18
    public class Servizio extends Service {
    19
        
    20
        private ArrayList<Location> poi = new ArrayList<Location>();
    21
        private Timer timer;
    22
        private String providerId = LocationManager.GPS_PROVIDER;
    23
        private Location location;
    24
        private LocationManager locationManager;
    25
        private LocationListener listener = new LocationListener() {
    26
            public void onStatusChanged(String provider, int status, Bundle extras) { 
    27
                 
    28
              } 
    29
               public void onProviderEnabled(String provider) { 
    30
             
    31
              } 
    32
               public void onProviderDisabled(String provider) { 
    33
            
    34
              } 
    35
               public void onLocationChanged(Location location) { 
    36
               
    37
              } 
    38
        };
    39
        
    40
        public IBinder onBind(Intent intent) { 
    41
               return null; 
    42
        } 
    43
        
    44
        public void onCreate(){
    45
            super.onCreate();
    46
            timer = new Timer();
    47
            timer.schedule(new Esegui(), 0, 60000);     
    48
        }
    49
        
    50
        public void onDestroy(){
    51
            timer.cancel();
    52
            timer = null;
    53
            try {
    54
                ObjectOutputStream oos = new ObjectOutputStream(openFileOutput("prova",Context.MODE_PRIVATE));
    55
                oos.writeObject(poi);
    56
                oos.close();
    57
                
    58
            } catch (FileNotFoundException e) {} catch (IOException e) {}
    59
        }
    60
        
    61
        private class Esegui extends TimerTask{
    62
            public void run(){
    63
                locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    64
                locationManager.requestLocationUpdates(providerId, 5, 15, listener);
    65
                location = locationManager.getLastKnownLocation(providerId);
    66
                poi.add(location);
    67
            }
    68
        }
    69
        
    70
    }
    Vi allego anche il log
    codice:
    08-26 18:11:53.075: W/dalvikvm(30915): threadid=11: thread exiting with uncaught exception (group=0x40c371f8)
    08-26 18:11:53.080: E/AndroidRuntime(30915): FATAL EXCEPTION: Timer-0
    08-26 18:11:53.080: E/AndroidRuntime(30915): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
    08-26 18:11:53.080: E/AndroidRuntime(30915): 	at android.os.Handler.<init>(Handler.java:121)
    08-26 18:11:53.080: E/AndroidRuntime(30915): 	at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:173)
    08-26 18:11:53.080: E/AndroidRuntime(30915): 	at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:173)
    08-26 18:11:53.080: E/AndroidRuntime(30915): 	at android.location.LocationManager._requestLocationUpdates(LocationManager.java:579)
    08-26 18:11:53.080: E/AndroidRuntime(30915): 	at android.location.LocationManager.requestLocationUpdates(LocationManager.java:446)
    08-26 18:11:53.080: E/AndroidRuntime(30915): 	at it.helloworld.tour.Servizio$Esegui.run(Servizio.java:64)
    08-26 18:11:53.080: E/AndroidRuntime(30915): 	at java.util.Timer$TimerImpl.run(Timer.java:284)
    08-26 18:12:21.040: I/Process(30915): Sending signal. PID: 30915 SIG: 9

  2.  

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire risposte
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Torna su
Privacy Policy