-3

Hi i know there are lot of answers to this topic. But I tried a lot and it doesn't work. I want to show a toast inside a thread of a service. How can i solve this problem. Using getApplicationContext() etc. doesn't work.

I start the Service from an Activity (no bounding).

public class CarDataService extends Service {
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    ...
    startThreadUpdatingDatabase();
    Toast.makeText(this, message, Toast.LENGTH_LONG).show(); //it works
    }

    private void startThreadUpdatingDatabase(){
        Log.d("Database", "startThreadUpdatingDatabase(was called)");
        new Thread(new Runnable() { 
            public void run(){
                ..
                // here i want to use a toast!!!
            }
        }).start();
    }

}

Thank you!

d4rty
  • 3,970
  • 5
  • 34
  • 73

4 Answers4

1

You have to start the thread:

new Thread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(),"Your message",Toast.LENGTH_LONG).show();
            }
   }).start();
Decoy
  • 1,598
  • 12
  • 19
  • This will give `java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()` – Vlad Sep 28 '18 at 12:53
1
public Contect context;

member variable

onStartCommand(){
context = getApplicationContext)
}

acquivre reference to the context before you start the thread

 new Thread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(context,"Your message",Toast.LENGTH_LONG).show();
                }
       }).start();

and there you go

use AsyncTask instead that helps in context management

http://www.androidsnippets.com/use-toast-wherever-you-want

  • Your solution doesn't work. When the app reaches the point Toast.makeText(context,"Your message",Toast.LENGTH_LONG).show(); the app crashes. – d4rty Dec 08 '14 at 15:11
  • yes this solution is flawed. it will try to show the toast in the context of the background thread. – Goran Horia Mihail Dec 08 '14 at 15:26
1

Show your Toast using UI-Thread

new Thread(new Runnable() {
    @Override
    public void run() {

        // SHOW TOAST
        activity.runOnUiThread(new Runnable() {
            public void run() {
                Toast.makeText(yourContext, "Hello from UI-thread", Toast.LENGTH_SHORT).show();
            }
        });

        //... start DB work

    }
}).start();

If you have no access to an activity, so do it this way:

new Thread(new Runnable() {
    @Override
    public void run() {

        // no activity, so use Handler & mainlooper
        new Handler(Looper.getMainLooper()).post(
            new Runnable() {
                public void run() {
                    // yourContext is Activity or Application context
                    Toast.makeText(yourContext, "Hello from UI-thread", Toast.LENGTH_SHORT).show();
                }
             }
        );

        //... start DB work

    }
}).start();

Look at this: Static Way to get Context on android?

Community
  • 1
  • 1
alex
  • 8,904
  • 6
  • 49
  • 75
  • What will i take for activity? I have several activities, but i cant take the class. And what for yourContext? – d4rty Dec 08 '14 at 15:18
  • I updated my answer. use Handler if you have no activity reference. – alex Dec 08 '14 at 15:28
1
 Handler h = new Handler(context.getMainLooper());

    h.post(new Runnable() {
        @Override
        public void run() {
             Toast.makeText(context,message,Toast.LENGTH_LONG).show();
        }
    });

see if this works out