Toast created in an IntentService never goes away

后端 未结 6 496
北海茫月
北海茫月 2020-11-28 10:53

I have an IntentService that downloads some files. The problem is that I create a Toast inside the IntentService like this

Toast.makeText(getApplicationCont         


        
相关标签:
6条回答
  • 2020-11-28 11:35

    You shouldn't create Toasts from a Service. You should use a Notification instead.

    0 讨论(0)
  • 2020-11-28 11:35

    The problem is that IntentService is not running on the main application thread. you need to obtain a Handler for the main thread (in onCreate()) and post the Toast to it as a Runnable.

    the following code should do the trick:

    @Override
    public void onCreate() {
        super.onCreate();
        mHandler = new Handler();
    }
    
    @Override
    protected void onHandleIntent(Intent intent) {
        mHandler.post(new Runnable() {            
            @Override
            public void run() {
                Toast.makeText(MyIntentService.this, "Hello Toast!", Toast.LENGTH_LONG).show();                
            }
        });
    }
    
    0 讨论(0)
  • 2020-11-28 11:48

    To show a toast when the user is in one of the application activity.

    Just need a reference of the current activity, and call it with this sample code:

    public void showToast(final String msg) {
        final Activity a = currentActivity;
        if (a != null ) {
            a.runOnUiThread(new Runnable() {
    
                @Override
                public void run() {
                    Toast.makeText(a, msg, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
    

    There is a lot of options to get the current activity, check this question: How to get current foreground activity context in android?

    But I use this approach:

    The application must have:

    private Activity currentActivity = null;
    
    public Activity getCurrentActivity() {
        return currentActivity;
    }
    
    public void setCurrentActivity(Activity mCurrentActivity) {
        this.currentActivity = mCurrentActivity;
    }
    

    Each activity must have:

    @Override
    protected void onResume() {
        super.onResume();
        ((MyApplication) getApplication()).setCurrentActivity(this);
    
    }
    @Override
    protected void onPause() {
        super.onPause();
        ((MyApplication) getApplication()).setCurrentActivity(null);
    }
    
    0 讨论(0)
  • 2020-11-28 11:50

    For people developing in Xamarin studio, this is how its done there:

    Handler handler = new Handler ();
    handler.Post (() => {
        Toast.MakeText (_Context, "Your text here.", ToastLength.Short).Show ();
    });
    
    0 讨论(0)
  • 2020-11-28 11:52

    This works for me:

    public void ShowToastInIntentService(final String sText) {
        final Context MyContext = this;
    
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                Toast toast1 = Toast.makeText(MyContext, sText, Toast.LENGTH_LONG);
                toast1.show();
            }
        });
    };
    
    0 讨论(0)
  • 2020-11-28 11:54

    IntentService will create a thread to handle the new intent, and terminated it immediately once the task has done. So, the Toast will be out of controlled by a dead thread.

    You should see some exceptions in the console when the toast showing on the screen.

    0 讨论(0)
提交回复
热议问题