从服务中的线程发送Toast

1 service multithreading android toast

我正在尝试在线程中发送错误的Toast通知.线程在从主线程调用的服务中启动.我已经尝试了几件事View.post和一些奇怪的处理程序的东西,但似乎没有任何工作.该帖子的摘录如下:

    public int onStartCommand(Intent intent, int flags, int startId) 
{
    new Thread(new Runnable(){
        public void run() {
            boolean bol = true;

            while (bol)
            {

                try 
                {
                    //Some socket code...
                } 
                catch (Exception e) 
                {
                 //Where I want the toast code.
                } 
            }

        }
    }).start();

    return START_STICKY;
}
Run Code Online (Sandbox Code Playgroud)

Sho*_*uri 6

尝试在服务中的线程内部进行以下操作:

Handler h = new Handler(context.getMainLooper());
     // Although you need to pass an appropriate context
    h.post(new Runnable() {
        @Override
        public void run() {
             Toast.makeText(context,message,Toast.LENGTH_LONG).show();
        }
    });
Run Code Online (Sandbox Code Playgroud)

从@Alex Gitelman给出的答案采取在这里的Android:我怎样才能显示在远程服务运行的线程举杯?.这可能对帮助我的人有所帮助.