Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Threads and Services Introducing Services Services Are Not Threads

The code does not work. The UI hangs up.

I really don't know why this happens. I think the code is exactly the same as what the video instructs. I am not a beginner programmer by the way. The main UI thread hangs up. I have included the Service in Manifest file. Do you have a working code uploaded somewhere like GitHub so I can run to check? The code up to this video lesson is fine.

Ben Deitch
Ben Deitch
Treehouse Teacher

On the 'Downloads' tab of that video you can download the project to compare with :)

2 Answers

Sachin Jayaram
Sachin Jayaram
5,461 Points

Hi Make sure in your MainActivity when you start the service you use startService(intent); and not startActivity(intent); Its a simple error anyone can make.

Piyush Ravi
Piyush Ravi
2,217 Points

I was facing the same issue. However when I closely inspected I found I was assigning the value and before starting the thread and while loop was something like this.

DownloadThread thread = new DownloadThread();
        handler = thread.mHandler; // mHandler not instantiated as thread has not started
        thread.setName("download thread");
        thread.start();
        while(handler == null) { // handler will always be null
           // this goes into infinite loop
        }
      // correct solution below

        thread.setName("download thread");
        thread.start();
        while(thread.mHandler == null) {

        }
        handler = thread.mHandler;