Add silent mode for Android

Fixes issue #39 and #48
This commit is contained in:
Sebastián Katzer 2015-01-01 17:24:37 +01:00
parent 0b9854e1bc
commit 270abbff02
4 changed files with 36 additions and 9 deletions

View File

@ -1,4 +1,7 @@
## ChangeLog ## ChangeLog
#### Version 0.6.3 (not yet released)
- [feature:] Silent mode for Android
#### Version 0.6.2 (14.12.2014) #### Version 0.6.2 (14.12.2014)
- [bugfix:] Type error - [bugfix:] Type error
- [bugfix:] Wrong default values for `isEnabled` and `isActive`. - [bugfix:] Wrong default values for `isEnabled` and `isActive`.

View File

@ -68,16 +68,13 @@ More informations can be found [here][PGB_plugin].
## ChangeLog ## ChangeLog
#### Version 0.6.3 (not yet released)
- [feature:] Silent mode for Android
#### Version 0.6.2 (14.12.2014) #### Version 0.6.2 (14.12.2014)
- [bugfix:] Type error - [bugfix:] Type error
- [bugfix:] Wrong default values for `isEnabled` and `isActive`. - [bugfix:] Wrong default values for `isEnabled` and `isActive`.
#### Version 0.6.1 (14.12.2014)
- [enhancement:] Set default settings through `setDefaults`.
- [enhancement:] New method `isEnabled` to receive if mode is enabled.
- [enhancement:] New method `isActive` to receive if mode is active.
- [bugfix:] Events caused thread collision.
#### Further informations #### Further informations
- The former `plugin.backgroundMode` namespace has been deprecated and will be removed with the next major release. - The former `plugin.backgroundMode` namespace has been deprecated and will be removed with the next major release.
- See [CHANGELOG.md][changelog] to get the full changelog for the plugin. - See [CHANGELOG.md][changelog] to get the full changelog for the plugin.
@ -229,6 +226,15 @@ cordova.plugins.backgroundMode.configure({
}) })
``` ```
#### Run in background without notification
In silent mode the plugin will not display a notification - which is not the default. Be aware that Android recommends adding a notification otherwise the OS may pause the app.
```javascript
cordova.plugins.backgroundMode.configure({
silent: true
})
```
## Contributing ## Contributing

View File

@ -36,6 +36,7 @@ import android.content.res.Resources;
import android.os.Build; import android.os.Build;
import android.os.Handler; import android.os.Handler;
import android.os.IBinder; import android.os.IBinder;
import android.util.Log;
/** /**
* Puts the service in a foreground state, where the system considers it to be * Puts the service in a foreground state, where the system considers it to be
@ -84,7 +85,11 @@ public class ForegroundService extends Service {
public void keepAwake() { public void keepAwake() {
final Handler handler = new Handler(); final Handler handler = new Handler();
startForeground(NOTIFICATION_ID, makeNotification()); if (!this.inSilentMode()) {
startForeground(NOTIFICATION_ID, makeNotification());
} else {
Log.w("BackgroundMode", "In silent mode app may be paused by OS!");
}
BackgroundMode.deleteUpdateSettings(); BackgroundMode.deleteUpdateSettings();
@ -160,7 +165,7 @@ public class ForegroundService extends Service {
* @return * @return
* The resource ID of the app icon * The resource ID of the app icon
*/ */
private int getIconResId () { private int getIconResId() {
Context context = getApplicationContext(); Context context = getApplicationContext();
Resources res = context.getResources(); Resources res = context.getResources();
String pkgName = context.getPackageName(); String pkgName = context.getPackageName();
@ -170,4 +175,16 @@ public class ForegroundService extends Service {
return resId; return resId;
} }
/**
* In silent mode no notification has to be added.
*
* @return
* True if silent: was set to true
*/
private boolean inSilentMode() {
JSONObject settings = BackgroundMode.getSettings();
return settings.optBoolean("silent", false);
}
} }

View File

@ -64,7 +64,8 @@ exports._defaults = {
title: 'App is running in background', title: 'App is running in background',
text: 'Doing heavy tasks.', text: 'Doing heavy tasks.',
ticker: 'App is running in background', ticker: 'App is running in background',
resume: true resume: true,
silent: false
}; };