【问题标题】:Show Toast in a Thread of a Service在服务的线程中显示 Toast
【发布时间】:2014-12-08 14:46:47
【问题描述】:

嗨,我知道这个话题有很多答案。但是我尝试了很多,它不起作用。我想在服务的线程中显示祝酒词。我怎么解决这个问题。使用 getApplicationContext() 等不起作用。

我从 Activity 启动服务(无边界)。

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

}

谢谢!

【问题讨论】:

  • 您无法从非 ui 线程显示 Toast。不要对明显正确的答案投反对票。
  • 您只是对答案投了反对票,因为您没有从中受益吗?我想知道谁以这种方式对所有答案投了反对票?

标签: android multithreading service toast


【解决方案1】:

你必须启动线程:

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

【讨论】:

  • 这将给java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
【解决方案2】:
public Contect context;

成员变量

onStartCommand(){
context = getApplicationContext)
}

在启动线程之前获取对上下文的引用

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

你去吧

使用 AsyncTask 代替,这有助于上下文管理

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

【讨论】:

  • 您的解决方案不起作用。当应用程序到达点 Toast.makeText(context,"Your message",Toast.LENGTH_LONG).show();应用崩溃。
  • 是的,这个解决方案是有缺陷的。它将尝试在后台线程的上下文中显示 toast。
【解决方案3】:
 Handler h = new Handler(context.getMainLooper());

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

看看有没有效果

【讨论】:

    【解决方案4】:

    使用 UI-Thread 展示你的 Toast

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

    如果您无权访问某项活动,请按以下方式进行操作:

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

    看看这个:Static Way to get Context on android?

    【讨论】:

    • 我要参加什么活动?我有几项活动,但我不能上课。你的上下文呢?
    • 我更新了我的答案。如果您没有活动参考,请使用 Handler。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-11
    • 2011-03-09
    • 2015-02-06
    相关资源
    最近更新 更多