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 3457514
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:52:09+00:00 2026-05-18T09:52:09+00:00

I’ve implemented a simple Observer pattern in a hello world style app. I’ve got

  • 0

I’ve implemented a simple Observer pattern in a “hello world” style app.

I’ve got 2 buttons, which call onStart() and onDestroy() on a Service.

In the onStart() I register an observer and then get my observer to run a for loop calling observe.update() once every second for 10 seconds. This in turn calls an update() method in my service.

I’m trying to display some dummy data which gets returned, but I’m getting the following error :

11-26 23:21:12.829: ERROR/AndroidRuntime(676): FATAL EXCEPTION: Thread-8
        java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
        at android.os.Handler.<init>(Handler.java:121)
        at android.widget.Toast.<init>(Toast.java:68)
        at android.widget.Toast.makeText(Toast.java:231)
        at com.jameselsey.observerpattern.LocalService.observe(LocalService.java:27)
        at obs.Stub$1.run(Stub.java:49)
        at java.lang.Thread.run(Thread.java:1096)

I’m doing the following in my service :

public void observe(String message)
    {
        Context context = getApplicationContext();
        Log.d("TEST", "Inside observe() on service, value is " + message);

        Toast toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
        toast.show();
    }

After a bit of research, it seems I can’t create a Toast message as this method doesn’t run on the main UI thread.

Is there anyway I can spit out message onto my UI? I’m just looking to have it display on the activitiy, even a regular textView is OK.

  • 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-05-18T09:52:10+00:00Added an answer on May 18, 2026 at 9:52 am

    Services are not supposed to show any UI. The only acceptable approach is to display a notification which can start an activity through an intent. Your service can also communicate with your UI using a BroadcastReceiver or it can start an activity directly.

    If somehow your activity gets called on a background thread, you can get it running on the main thread using runOnUiThread. You should also get intimately familiar with services in general.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And
I am doing a simple coin flipping experiment for class that involves flipping a
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I would like to run a str_replace or preg_replace which looks for certain words

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.