package com.hfad.joke; import android.app.IntentService; import android.content.Intent; import android.os.Handler; import android.widget.Toast; public class DelayedMessageService extends IntentService { public static final String EXTRA_MESSAGE = "message"; private Handler handler; public DelayedMessageService() { super("DelayedMessageService"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // This method runs on the main thread, so the Handler that's // instantiated here will also be running on the main thread. handler = new Handler(); return super.onStartCommand(intent, flags, startId); } @Override protected void onHandleIntent(Intent intent) { synchronized (this) { try { wait(10000); } catch (InterruptedException e) { e.printStackTrace(); } } String text = intent.getStringExtra(EXTRA_MESSAGE); showText(text); } private void showText(final String text) { handler.post(new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show(); } }); } }