线程完成执行时不会出现Toast

时间:2016-05-03 13:30:28

标签: android multithreading android-studio

我的Android应用程序中有一个Do While Filename <> "" Application.ScreenUpdating = False Set wb = Workbooks.Open(folderPath & Filename) wb.Range(wb.Cells(1,"B"), wb.Cells(Rows.Count,"B").End(xlUp)).Copy Workbooks("Compiled.xlsm").Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Transpose:=True wb.Close True Filename = Dir Loop 个帖子。我希望在此线程完成执行时通过AsyncTask<Task, Void, Boolean>显示消息。为此,我在Toast.makeText()内添加了Toask.makeText()以及在if方法的else内添加了doInBackground。该线程正在成功完成其执行,但是没有出现Toast的消息。那可能是什么问题?

代码:

@Override
    protected Boolean doInBackground(Task... arg0) {
        try {

            Task task = arg0[0];

            QueryBuilder qb = new QueryBuilder();

            HttpClient httpClient = new DefaultHttpClient();
            HttpPost request = new HttpPost(qb.buildContactsSaveURL());

            StringEntity params =new StringEntity(qb.createTask(task));
            request.addHeader("content-type", "application/json");
            request.setEntity(params);
            HttpResponse response = httpClient.execute(request);

            if(response.getStatusLine().getStatusCode()<205)
            {
                /*this is the message inside if*/
                Toast.makeText(context, "inside -IF", Toast.LENGTH_SHORT).show();
                return true;
            }
            else
            {
                /*this is the message inside else*/
                Toast.makeText(context, "inside -ELSE", Toast.LENGTH_SHORT).show();
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

4 个答案:

答案 0 :(得分:3)

主线程中的Toast工作,您试图在后台线程(doInBackground)中显示Toast。将您的toast代码移至onPostExecution callaback,您将能够看到Toasts。

答案 1 :(得分:0)

它正在进行的任务是在后台,它不会在背景中显示吐司。 后台任务不会影响您的UI或主线程。

答案 2 :(得分:0)

  

线程正在成功完成其执行但是吐司   消息未出现

因为doInBackground方法在非ui-Thread上运行。和应用程序仅显示来自UI-Thread的警报,Toast和更新UI元素。

doInBackground方法

中显示来自runOnUiThread Toast相关代码的Toast

response方法返回doInBackground并使用onPostExecute方法显示Toast。

答案 3 :(得分:0)

正如其他人所提到的,您不应该在后台线程上进行任何与UI相关的更改/活动。在onPostExecute方法执行的主线程上执行此操作。这是一个例子

private class DoSomethingTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        //Do background process here. Make sure there are no UI related changes here
        return null;
    }

    protected void onPostExecute(Void x) 
    {
        //Do UI related changes here
    }
}

使用您的代码:

private class DoSomethingTask extends AsyncTask<Void, Void, Void> {
    int statusCode;
    @Override
    protected Void doInBackground(Task... arg0) {
        try {

        Task task = arg0[0];

        QueryBuilder qb = new QueryBuilder();

        HttpClient httpClient = new DefaultHttpClient();
        HttpPost request = new HttpPost(qb.buildContactsSaveURL());

        StringEntity params =new StringEntity(qb.createTask(task));
        request.addHeader("content-type", "application/json");
        request.setEntity(params);
        HttpResponse response = httpClient.execute(request);
        statusCode = response.getStatusLine().getStatusCode();

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    protected void onPostExecute(Void x) 
    {
        //Do UI related changes here

        if(statusCode < 205)
        {
            /*this is the message inside if*/
            Toast.makeText(context, "inside -IF", Toast.LENGTH_SHORT).show();
            return true;
        }
        else
        {
            /*this is the message inside else*/
            Toast.makeText(context, "inside -ELSE", Toast.LENGTH_SHORT).show();
            return false;
        }
    }
}

希望这有帮助!