Błąd wywoływania toastu z usługi Android [duplikat]
Możliwy duplikat:
Android Toast uruchomiony z usługi wyświetla tylko raz
Używam usługi Android zdefiniowanej w android.app.Service.
Nazywam tę usługę (myService) z działania.
MyService to:
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();
}
}
Metoda checkDate () znajduje się w klasie myService.
Wystąpił błąd:
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)