From 0994147d1344629eda14ff14a698bf562f7ab373 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Katzer?= Date: Sun, 14 Dec 2014 13:23:10 +0100 Subject: [PATCH] Fix main thread bug --- src/android/BackgroundMode.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/android/BackgroundMode.java b/src/android/BackgroundMode.java index 3c4fe34..e0cb187 100644 --- a/src/android/BackgroundMode.java +++ b/src/android/BackgroundMode.java @@ -42,6 +42,9 @@ public class BackgroundMode extends CordovaPlugin { ACTIVATE, DEACTIVATE, FAILURE } + // Plugin namespace + private static final String JS_NAMESPACE = "cordova.plugins.backgroundMode"; + // Flag indicates if the app is in background or foreground private boolean inBackground = false; @@ -247,10 +250,14 @@ public class BackgroundMode extends CordovaPlugin { } - String js = String.format("setTimeout('cordova.plugins.backgroundMode" + - ".on%s(%s)',0)", - eventName, params); + final String js = String.format("setTimeout('%s.on%s(%s)',0)", + JS_NAMESPACE, eventName, params); - webView.loadUrl("javascript:" + js); + cordova.getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + webView.loadUrl("javascript:" + js); + } + }); } }