Closed askeypuah32 closed 10 years ago
That is because a Toast needs to run on UI thread. Try this method (include it in your service java file).
private void showToast(final String message, final String duration) { handler.post(new Runnable() { @Override public void run() { if(duration.equals("long")) { Toast.makeText(getBaseContext(), message, Toast.LENGTH_LONG).show(); } else { Toast.makeText(getBaseContext(), message, Toast.LENGTH_SHORT).show(); } } }); }
And call it by this: showToast("message", "short");
And this line in your code in the top: final Handler handler = new Handler();
Great! It's works! Thanks a lot!
Alternatively use Log.d() to output to the logcat screen.
By their very nature, background services shouldn't be doing anything with the foreground - Toast included.
Hi,
In the background service, I tried to use toast to show some message but failed. The toast message didn't show.
Thanks,
Puah.