Android: Toast from IntentService remains on screen for ever

ぐ巨炮叔叔 提交于 2019-12-06 03:16:38
Jeff G

The article linked to in my edit led me to this blog which fixed my problem. I had not seen elsewhere that you cannot issue a toast directly from a service. So my onHandleIntent now looks like this:

private Handler handler;

@Override
protected void onHandleIntent(Intent intent)
{
    Log.d(TAG, "onHandleIntent");

    /* Stripped out some code here that I added later to keep it similar to
     * my example above
     */

    handler.post(new Runnable()
    {  
//      @Override
        public void run()
        {
            Toast.makeText(getApplicationContext(), "File has been saved", 
                Toast.LENGTH_SHORT).show();
        }
    });    // Display toast and exit
}

(If anyone can explain why I had to comment out the @Override on the run() to avoid an error, I would be grateful. There are several other places in my application that I have had to do that.)

Edit: I have only just found this post which is virtually the same thing. I don't know how I missed it before.

onHandleIntent is executed in a worker thread.

I would try to execute Toast.show() from a UI thread.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!