Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8549881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:49:21+00:00 2026-06-11T13:49:21+00:00

Possible Duplicate: Android Toast started from Service only displays once I’m using Service Android

  • 0

Possible Duplicate:
Android Toast started from Service only displays once

I’m using Service Android defined in android.app.Service.

I call this Service (myService) from an Activity.

MyService is:

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();
}

}

The method checkDate() resides in the class myService.

The error produced is:

 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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:49:22+00:00Added an answer on June 11, 2026 at 1:49 pm

    TimerTask runs in a separate thread. Toast.makeText() must be executed from a thread that has established a Handler/Looper. Basically this means you need to make the toast on a thread that has the standard Android message/event dispatcher running in it.

    Easiest way to do this would be in your checkDate() method:

    runOnUiThread(new Runnable() {
        public void run() {
            Toast toast = Toast.makeText(this, "SIMPLE MESSAGE!", Toast.LENGTH_LONG);
            toast.show();
        }
     });
    

    EDIT: I’m an idiot, that’s not right. You can’t call runOnUiThread() from a Service context

    You need to use a Handler for this. In your service:

    private Handler handler;
    

    in onCreate() of your service:

    handler = new Handler();
    

    in checkDate() method:

    handler.post(new Runnable() {
        public void run() {
            Toast toast = Toast.makeText(myService.this, "SIMPLE MESSAGE!", Toast.LENGTH_LONG);
            toast.show();
        }
     });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Android: How can I get the current foreground activity (from a service)?
Possible Duplicate: Android Launch an application from another application I am having a problem
Possible Duplicate: Slow Android emulator I am using Eclipse to load the Android project
Possible Duplicate: Android Launch an application from another application I want to start Another
Possible Duplicate: Why does Android app run in small window on tablet emulator? i
Possible Duplicate: Sending Email in Android using JavaMail API without using the default android
Possible Duplicate: Android and XMPP: Currently available solutions I've started prototyping a multiplayer Android
Possible Duplicate: Android Unknown Command 'crunch' I have been using eclipse just fine but
Possible Duplicate: Where does Android emulator store SQLite database? I am using SQLite and
Possible Duplicate: Android - how to set the wallpaper image I hav started doing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.