繁体   English   中英

线程完成其执行时不显示Toast

[英]Toast does not appears when thread completes its execution

我的Android应用程序中有一个AsyncTask<Task, Void, Boolean>线程。 我想在此线程完成其执行时通过Toast.makeText()显示消息。 为此,我已经添加Toask.makeText()if还有内部elsedoInBackground方法。 该线程已成功完成其执行,但没有出现吐司的消息。 那么可能是什么问题呢?

码:

@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;
        }
    }

您尝试在主线程中显示Toast的后台线程 (doInBackground)。 将您的Toast代码移至onPostExecution回调,您将可以看到Toasts。

它正在执行的任务在后台,不会像在后台那样显示吐司。 后台任务不会影响您的UI或主线程。

线程已成功完成其执行,但没有出现吐司消息

因为doInBackground方法在非ui线程上运行。 和应用程序仅显示Alert,Toast并仅从UI-Thread更新UI元素。

doInBackground显示Toast在runOnUiThread方法中包装Toast相关代码

要么

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

正如其他人所提到的,您不应在后台线程上进行任何与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;
        }
    }
}

希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM