Android: Toast from IntentService remains on screen for ever

*爱你&永不变心* 提交于 2020-01-02 07:32:33

问题


I checked this question, but it doesn't appear to answer my problem, which is much less involved. I am calling an IntentService from a menu item in my main process. It is currently just a skeleton which puts up a Toast in onHandleIntent() which eventually should appear briefly at the end of the processing to say it's finished. However it remains on the screen for ever (even when the application is stopped). I tested both with the emulator and my Galaxy S2 with identical results. Can someone point me in the right direction, please?

This is the service code:

 package com.enborne.spine;

 import android.app.IntentService;
 import android.content.Intent;
 import android.util.Log;
 import android.widget.Toast;

 public class SaveFile extends IntentService {
     private final String TAG = "SaveFile";

     public SaveFile() {
         super("SaveFile");
     }

     @Override
     public void onCreate() {
         super.onCreate();
         Log.d(TAG, "Service Started.. ");
     }

     @Override
     public void onDestroy() {
         super.onDestroy();
         Log.d(TAG, "Service Destroyed.. ");
     }

     @Override
     protected void onHandleIntent(Intent intent) {
         // TODO Auto-generated method stub
         Log.d(TAG, "HandleIntent");
         // File saved
         Toast.makeText(this, "File has been saved", Toast.LENGTH_SHORT).show();
     }
 }

(The onCreate and onDestroy overrides are only there temporarily for logging in Eclipse so I can see what's happening.)

I invoke it from a menu in an activity thus (the messing with context was only because the service didn't start - some idiot forgot to include it in the manifest!):

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Intent i;
    switch (item.getItemId()) {
    case R.id.chart:
        i = new Intent(getBaseContext(), Chart.class);
        i.putExtra(KEY_CENTRE_LAT, mCentreLat); // centre coordinate
        i.putExtra(KEY_CENTRE_LONG, mCentreLong); // centre coordinate
        i.putExtra(KEY_RADIUS, mRadius); // effective radius
        startActivity(i);
        break;

    case R.id.save:
        Log.d("SaveFile", "Starting service...");
        //      i = new Intent(getBaseContext(), SaveFile.class);
        i = new Intent(this, SaveFile.class);
        startService(i);
        break;
    }
    return true;
}

EDIT: I found this article, which may explain the problem. To quote one sentence:

However, you can only use Toast in the main GUI thread, or else you run into problems where the Toast message doesn't disappear after a period (because the main GUI context doesn't know anything about Toast messages used in a separate thread context).


回答1:


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.




回答2:


onHandleIntent is executed in a worker thread.

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



来源:https://stackoverflow.com/questions/12407910/android-toast-from-intentservice-remains-on-screen-for-ever

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