Originariamente inviato da
snieco
Ciao a tutti, ho questa necessità: ho un service attivo sul terminale android che scarica degli eventi da un webservice. Vorrei che gli eventi venissero scaricati periodicamente in seguito ad uno spostamento del terminale.
Ho realizzato questo:
codice:
public void onCreate() {
contentView = new RemoteViews(getPackageName(), R.layout.notification);
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
lm = (LocationManager) getSystemService(LOCATION_SERVICE);
lm1 = (LocationManager) getSystemService(LOCATION_SERVICE);
usersettings = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, (long) 0, 0.0f, new GPSListener());
Thread notifyingThread = new Thread(null, mainthread, "NotifyingService");
Thread gpsNotificatorThread = new Thread(null, gpsthread, "NotifyingGPSChange");
condvar = new ConditionVariable(false);
condvargps = new ConditionVariable(false);
downloadlock = new ConditionVariable(false);
notifyingThread.start();
gpsNotificatorThread.start();
}
dove creo due locationmanager (uno per un thread e uno per un altro)
I due tread sono:
codice:
private Runnable gpsthread = new Runnable() {
public void run() {
Log.i(TAG, "gpsThread started");
while (true) {
int mindist = Integer.parseInt(usersettings.getString("queryperiod", "100"));
lm1.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, (float)mindist,
new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.i(TAG, "Location changed beyond the min distance");
condvargps.open();
}
});
}
}
};
e
codice:
private Runnable mainthread = new Runnable() {
public void run() {
while (true){
// get preference on query period, return default 5 min if not set
//int delay = Integer.parseInt(usersettings.getString("queryperiod", "300")) * 1000;
int delay = Integer.parseInt(usersettings.getString("queryperiod", "100"));
if (delay == 0) {
Log.i(TAG, "Thread blocked for 60000 millis");
condvar.block(60000);
continue;
}
Log.i(TAG, "distance query is "+delay+" mt");
condvargps.block();
...
...
e successivamente mi scarica gli eventi.
Ho creato il thread mainthread che si blocca. Posso scegliere la distanza minima di refresh degli eventi, se è zero si blocca subito per 60000 millis, altrimenti si blocca subito dopo e resta in attesa che il thread gpsthread faccia l'open() sulla ConditionVariable.
Il codice non mi da errore, ma quando lo faccio partire, mi restituisce una eccezione:
codice:
ERROR/AndroidRuntime(495): FATAL EXCEPTION: NotifyingGPSChange
ERROR/AndroidRuntime(495): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
ERROR/AndroidRuntime(495): at android.os.Handler.<init>(Handler.java:121)
ERROR/AndroidRuntime(495): at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:139)
ERROR/AndroidRuntime(495): at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:137)
ERROR/AndroidRuntime(495): at android.location.LocationManager._requestLocationUpdates(LocationManager.java:708)
ERROR/AndroidRuntime(495): at android.location.LocationManager.requestLocationUpdates(LocationManager.java:630)
ERROR/AndroidRuntime(495): at com.thesisug.notification.TaskNotification$1.run(TaskNotification.java:90)
ERROR/AndroidRuntime(495): at java.lang.Thread.run(Thread.java:1096)
la riga 90 del TaskNotificator è questa:
codice:
lm1.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, (float)mindist, new LocationListener() {
con l'implementazione del LocationListener.
Cosa può essere? Cos'è che sbaglio? E' la prima volta che faccio una cosa del genere e vorrei imparare cosa c'è che non va...Eventualmente come può essere risolto questo problema? C'è un metodo più veloce piuttosto che creare un thread che me ne risveglia un altro?
grazie a tutti
Alessio