Possible Duplicate:
Android Toast started from Service only displays onceerrore chiamando brindisi dal Servizio Android
Sto utilizzando il servizio di Android definito android.app.Service.
Io chiamo questo servizio (myService) da un'attività.
MyService è:
public class myService extends Service{
public IBinder onBind(Intent intent){
return null;
}
public void onCreate(){
super.onCreate();
TimerTask task = new TimerTask(){
public void run(){
Log.i("test","service running");
checkDate();
}
};
timer = new Timer();
timer.schedule(task, 0, 20000);
}
public void checkDate(){
Toast toast = Toast.makeText(this, "SIMPLE MESSAGE!", Toast.LENGTH_LONG);
toast.show();
}
}
Il metodo checkdate() risiede nel myService di classe.
L'errore prodotta è:
09-19 15:41:35.267: E/AndroidRuntime(2026): FATAL EXCEPTION: Timer-0
09-19 15:41:35.267: E/AndroidRuntime(2026): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
09-19 15:41:35.267: E/AndroidRuntime(2026): at android.os.Handler.<init>(Handler.java:121)
09-19 15:41:35.267: E/AndroidRuntime(2026): at android.widget.Toast$TN.<init>(Toast.java:310)
09-19 15:41:35.267: E/AndroidRuntime(2026): at android.widget.Toast.<init>(Toast.java:84)
09-19 15:41:35.267: E/AndroidRuntime(2026): at android.widget.Toast.makeText(Toast.java:226)
potete inserire il codice che si utilizza per iniziare questo? Inoltre questo è un vero servizio Android o è solo un thread in background? Il problema è che non puoi interagire con l'interfaccia utente da un thread in background. Se hai un riferimento al contesto di attività puoi chiamare runOnUIThread usando quel contesto. – Bobbake4