A toast created in an IntentService never leaves

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

Toast.makeText(getApplicationContext(), "some message", Toast.LENGTH_SHORT).show(); 

The toast will never disappear if I exit the application. The only way to destroy it is to kill the process.

What am I doing wrong?

+27
android
Jul 21 2018-10-21T00:
source share
6 answers

You should not create Toast from Service . Instead, use Notification .

-7
Jul 21 '10 at 22:15
source share

The problem is that the IntentService not working in the main thread of the application. you need to get a Handler for the main thread (in onCreate() ) and send Toast to it as 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(); } }); } 
+69
Mar 24 2018-11-21T00:
source share

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(); } }); }; 
+17
Aug 13 '13 at 16:48
source share

IntentService will create a thread to handle the new intent and immediately terminate it immediately after the task completes. Thus, the toast will not be controlled by dead thread.

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

+8
May 27 '13 at 3:59
source share

For those who are developing in the Xamarin studio, here's how to do it:

 Handler handler = new Handler (); handler.Post (() => { Toast.MakeText (_Context, "Your text here.", ToastLength.Short).Show (); }); 
+1
Dec 02 '14 at 6:18
source share

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

You just need a link to the current activity and call it using this code example:

 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 are many options for getting current activity, check this question: How to get the 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 action must have:

 @Override protected void onResume() { super.onResume(); ((MyApplication) getApplication()).setCurrentActivity(this); } @Override protected void onPause() { super.onPause(); ((MyApplication) getApplication()).setCurrentActivity(null); } 
0
Jun 13 '13 at 21:35
source share



All Articles