Update notification when calling configure in background

This commit is contained in:
Sebastián Katzer 2014-11-04 16:59:33 +00:00
parent 8b7ef48cff
commit 3cf8b62e84
2 changed files with 32 additions and 16 deletions

View File

@ -21,6 +21,12 @@
package de.appplant.cordova.plugin.background; package de.appplant.cordova.plugin.background;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity; import android.app.Activity;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
@ -29,12 +35,6 @@ import android.content.ServiceConnection;
import android.os.IBinder; import android.os.IBinder;
import android.util.Log; import android.util.Log;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class BackgroundMode extends CordovaPlugin { public class BackgroundMode extends CordovaPlugin {
// Flag indicates if the app is in background or foreground // Flag indicates if the app is in background or foreground
@ -47,7 +47,7 @@ public class BackgroundMode extends CordovaPlugin {
static JSONObject settings = new JSONObject(); static JSONObject settings = new JSONObject();
// Used to (un)bind the service to with the activity // Used to (un)bind the service to with the activity
private ServiceConnection connection = new ServiceConnection() { private final ServiceConnection connection = new ServiceConnection() {
@Override @Override
public void onServiceConnected(ComponentName name, IBinder binder) { public void onServiceConnected(ComponentName name, IBinder binder) {
@ -85,7 +85,8 @@ public class BackgroundMode extends CordovaPlugin {
} }
if (action.equalsIgnoreCase("configure")) { if (action.equalsIgnoreCase("configure")) {
settings = args.getJSONObject(0); setSettings(args.getJSONObject(0));
return true; return true;
} }
@ -154,6 +155,20 @@ public class BackgroundMode extends CordovaPlugin {
isDisabled = true; isDisabled = true;
} }
/**
* Update the settings and maybe the notification.
*
* @param settings
*/
private void setSettings(JSONObject settings) {
this.settings = settings;
if (inBackground) {
stopService();
startService();
}
}
/** /**
* Bind the activity to a background service and put them into foreground * Bind the activity to a background service and put them into foreground
* state. * state.
@ -164,8 +179,9 @@ public class BackgroundMode extends CordovaPlugin {
Intent intent = new Intent( Intent intent = new Intent(
context, ForegroundService.class); context, ForegroundService.class);
if (isDisabled) if (isDisabled) {
return; return;
}
context.bindService( context.bindService(
intent, connection, Context.BIND_AUTO_CREATE); intent, connection, Context.BIND_AUTO_CREATE);