Red-Folder / Cordova-Plugin-BackgroundService

BackroundService plugin for use with Cordova (PhoneGap)
144 stars 85 forks source link

Toast #28

Closed askeypuah32 closed 10 years ago

askeypuah32 commented 10 years ago

Hi,

In the background service, I tried to use toast to show some message but failed. The toast message didn't show.

Thanks,

Puah.

teusink commented 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");

teusink commented 10 years ago

And this line in your code in the top: final Handler handler = new Handler();

askeypuah32 commented 10 years ago

Great! It's works! Thanks a lot!

Red-Folder commented 10 years ago

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.