土司onPostExecute()在android系统 [英] Toast in onPostExecute() in android

查看:135
本文介绍了土司onPostExecute()在android系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想张贴在OnPostExecture敬酒在异步。

I am trying to post a toast in OnPostExecture in an Async.

下面是我做了什么,但它不记录,它不敬酒。

Here is the what I have done but it doesn't log and it is not toasting.

protected class sendDetails extends AsyncTask<Context, Integer, Boolean>
{

    @Override
    protected Boolean doInBackground(Context... params) 
    {
        try 
        {
            Thread.sleep(1000);
        }
        catch (InterruptedException e) 
        {
        }

        sendEmail();
        return true;
    }

    @Override
    protected void onPostExecute(Boolean result) 
    {
        runOnUiThread(new Runnable() 
        {
            public void run() 
            {
                Toast.makeText(mContext, "Email Succesfully Sent...", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void onPreExecute() 
    {
        super.onPreExecute();
        finish();
    }
}

我也试过以下内容:

I also tried the following:

声明上下文:

 private Context mContext;

//调用异步

new sendDetails(mContext).execute();

//异步code:

//Async Code:

protected class sendDetails extends AsyncTask<Context, Integer, Boolean>
{

    public sendDetails(final Context context) 
    {
        mContext = context;
    }

    @Override
    protected Boolean doInBackground(Context... params) 
    {
        try 
        {
            Thread.sleep(1000);
        }
        catch (InterruptedException e) 
        {
        }

        sendEmail();
        return true;
    }

    @Override
    protected void onPostExecute(Boolean result) 
    {
        runOnUiThread(new Runnable() 
        {
            public void run() 
            {
                Toast.makeText(mContext, "Email Succesfully Sent...", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void onPreExecute() 
    {
        super.onPreExecute();
        finish();
    }
}

两人都没有工作。如何从异步敬酒..有人可以帮我解决这个问题?

Both didn't work. How to toast from Async.. Can somebody help me fix this?

更新:

根据JDJ,matiash&安培更改; RajaReddy PolamReddy

Changed according to JDJ, matiash & RajaReddy PolamReddy

删除完成();在preExecute。
并增加了在OnPostExecute以下内容:

remove Finish(); in onPreExecute. And added the following in OnPostExecute:

 Toast.makeText(MainActivity.this, "Email Succesfully Sent...", Toast.LENGTH_SHORT).show();
 finish();

谢谢!

推荐答案

您不必调用 runOnUiThread() onPostExecute() ......它已经在该点的UI线程上。

You don't have to call runOnUiThread() in onPostExecute() ... it's already on the UI thread at that point.

所以,你onPostExecute()应该只是这样的:

So your onPostExecute() should just look like this:

@Override
protected void onPostExecute(Boolean result) 
{
    Toast.makeText(mContext, 
                   "Email Succesfully Sent...", 
                   Toast.LENGTH_SHORT).show();
}

和按完成()在ING在后台线程上开始前preExecute()的活动中,后台线程完成,onPostExecute()被调用后,中, mContext 在你传递到吐司构造,甚至可能不再有效onPostExecute()。

And by finish()ing your activity in onPreExecute() before the background thread starts, after the background thread is done and onPostExecute() is called, the mContext in onPostExecute() that you're passing into the Toast constructor might not even be valid anymore.

这篇关于土司onPostExecute()在android系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆