mirror of
https://bitbucket.org/TheBosZ/cordova-plugin-run-in-background
synced 2024-11-25 00:34:54 +00:00
Fix app going to sleep when device locked
This commit is contained in:
parent
6297b9aea3
commit
830c4a97ad
@ -3,7 +3,7 @@
|
|||||||
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
|
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
id="cordova-plugin-background-mode"
|
id="cordova-plugin-background-mode"
|
||||||
version="0.6.6.dev">
|
version="0.6.6-dev">
|
||||||
|
|
||||||
<name>BackgroundMode</name>
|
<name>BackgroundMode</name>
|
||||||
|
|
||||||
@ -72,7 +72,10 @@
|
|||||||
* candidate for killing when low on memory.
|
* candidate for killing when low on memory.
|
||||||
-->
|
-->
|
||||||
<service android:name="de.appplant.cordova.plugin.background.ForegroundService" />
|
<service android:name="de.appplant.cordova.plugin.background.ForegroundService" />
|
||||||
|
</config-file>
|
||||||
|
|
||||||
|
<config-file target="AndroidManifest.xml" parent="/manifest">
|
||||||
|
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
||||||
</config-file>
|
</config-file>
|
||||||
|
|
||||||
<source-file
|
<source-file
|
||||||
|
@ -21,12 +21,6 @@
|
|||||||
|
|
||||||
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;
|
||||||
@ -34,6 +28,12 @@ import android.content.Intent;
|
|||||||
import android.content.ServiceConnection;
|
import android.content.ServiceConnection;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
|
|
||||||
|
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 {
|
||||||
|
|
||||||
// Event types for callbacks
|
// Event types for callbacks
|
||||||
@ -230,15 +230,15 @@ public class BackgroundMode extends CordovaPlugin {
|
|||||||
private void startService() {
|
private void startService() {
|
||||||
Activity context = cordova.getActivity();
|
Activity context = cordova.getActivity();
|
||||||
|
|
||||||
Intent intent = new Intent(
|
|
||||||
context, ForegroundService.class);
|
|
||||||
|
|
||||||
if (isDisabled || isBind)
|
if (isDisabled || isBind)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
Intent intent = new Intent(
|
||||||
|
context, ForegroundService.class);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
context.bindService(
|
context.bindService(intent,
|
||||||
intent, connection, Context.BIND_AUTO_CREATE);
|
connection, Context.BIND_AUTO_CREATE);
|
||||||
|
|
||||||
fireEvent(Event.ACTIVATE, null);
|
fireEvent(Event.ACTIVATE, null);
|
||||||
|
|
||||||
@ -282,9 +282,6 @@ public class BackgroundMode extends CordovaPlugin {
|
|||||||
private void fireEvent (Event event, String params) {
|
private void fireEvent (Event event, String params) {
|
||||||
String eventName;
|
String eventName;
|
||||||
|
|
||||||
if (updateSettings != null && event != Event.FAILURE)
|
|
||||||
return;
|
|
||||||
|
|
||||||
switch (event) {
|
switch (event) {
|
||||||
case ACTIVATE:
|
case ACTIVATE:
|
||||||
eventName = "activate"; break;
|
eventName = "activate"; break;
|
||||||
|
@ -21,12 +21,6 @@
|
|||||||
|
|
||||||
package de.appplant.cordova.plugin.background;
|
package de.appplant.cordova.plugin.background;
|
||||||
|
|
||||||
import java.util.Timer;
|
|
||||||
import java.util.TimerTask;
|
|
||||||
|
|
||||||
import org.json.JSONObject;
|
|
||||||
|
|
||||||
import android.annotation.SuppressLint;
|
|
||||||
import android.app.Notification;
|
import android.app.Notification;
|
||||||
import android.app.NotificationManager;
|
import android.app.NotificationManager;
|
||||||
import android.app.PendingIntent;
|
import android.app.PendingIntent;
|
||||||
@ -34,13 +28,13 @@ import android.app.Service;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
import android.graphics.Color;
|
|
||||||
import android.os.Binder;
|
import android.os.Binder;
|
||||||
import android.os.Build;
|
|
||||||
import android.os.Handler;
|
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
|
import android.os.PowerManager;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
||||||
* something the user is actively aware of and thus not a candidate for killing
|
* something the user is actively aware of and thus not a candidate for killing
|
||||||
@ -51,16 +45,10 @@ public class ForegroundService extends Service {
|
|||||||
// Fixed ID for the 'foreground' notification
|
// Fixed ID for the 'foreground' notification
|
||||||
private static final int NOTIFICATION_ID = -574543954;
|
private static final int NOTIFICATION_ID = -574543954;
|
||||||
|
|
||||||
private Notification.Builder notification;
|
|
||||||
|
|
||||||
// Binder given to clients
|
// Binder given to clients
|
||||||
private final IBinder mBinder = new ForegroundBinder();
|
private final IBinder mBinder = new ForegroundBinder();
|
||||||
|
|
||||||
// Scheduler to exec periodic tasks
|
private PowerManager.WakeLock wakeLock;
|
||||||
final Timer scheduler = new Timer();
|
|
||||||
|
|
||||||
// Used to keep the app alive
|
|
||||||
TimerTask keepAliveTask;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Allow clients to call on to the service.
|
* Allow clients to call on to the service.
|
||||||
@ -91,6 +79,9 @@ public class ForegroundService extends Service {
|
|||||||
keepAwake();
|
keepAwake();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* No need to run headless on destroy.
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
@ -102,7 +93,6 @@ public class ForegroundService extends Service {
|
|||||||
* by the OS.
|
* by the OS.
|
||||||
*/
|
*/
|
||||||
public void keepAwake() {
|
public void keepAwake() {
|
||||||
final Handler handler = new Handler();
|
|
||||||
|
|
||||||
if (!this.inSilentMode()) {
|
if (!this.inSilentMode()) {
|
||||||
startForeground(NOTIFICATION_ID, makeNotification());
|
startForeground(NOTIFICATION_ID, makeNotification());
|
||||||
@ -112,20 +102,12 @@ public class ForegroundService extends Service {
|
|||||||
|
|
||||||
BackgroundMode.deleteUpdateSettings();
|
BackgroundMode.deleteUpdateSettings();
|
||||||
|
|
||||||
keepAliveTask = new TimerTask() {
|
PowerManager powerMgr = (PowerManager) getSystemService(POWER_SERVICE);
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
handler.post(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
// Nothing to do here
|
|
||||||
// Log.d("BackgroundMode", "" + new Date().getTime());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
scheduler.schedule(keepAliveTask, 0, 1000);
|
wakeLock = powerMgr.newWakeLock(
|
||||||
|
PowerManager.PARTIAL_WAKE_LOCK, "BackgroundMode");
|
||||||
|
|
||||||
|
wakeLock.acquire();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -133,7 +115,11 @@ public class ForegroundService extends Service {
|
|||||||
*/
|
*/
|
||||||
private void sleepWell() {
|
private void sleepWell() {
|
||||||
stopForeground(true);
|
stopForeground(true);
|
||||||
keepAliveTask.cancel();
|
|
||||||
|
if (wakeLock != null) {
|
||||||
|
wakeLock.release();
|
||||||
|
wakeLock = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -144,8 +130,6 @@ public class ForegroundService extends Service {
|
|||||||
* A local ongoing notification which pending intent is bound to the
|
* A local ongoing notification which pending intent is bound to the
|
||||||
* main activity.
|
* main activity.
|
||||||
*/
|
*/
|
||||||
@SuppressLint("NewApi")
|
|
||||||
@SuppressWarnings("deprecation")
|
|
||||||
private Notification makeNotification() {
|
private Notification makeNotification() {
|
||||||
JSONObject settings = BackgroundMode.getSettings();
|
JSONObject settings = BackgroundMode.getSettings();
|
||||||
Context context = getApplicationContext();
|
Context context = getApplicationContext();
|
||||||
@ -153,49 +137,32 @@ public class ForegroundService extends Service {
|
|||||||
Intent intent = context.getPackageManager()
|
Intent intent = context.getPackageManager()
|
||||||
.getLaunchIntentForPackage(pkgName);
|
.getLaunchIntentForPackage(pkgName);
|
||||||
|
|
||||||
notification = new Notification.Builder(context)
|
Notification.Builder notification = new Notification.Builder(context)
|
||||||
.setContentTitle(settings.optString("title", ""))
|
.setContentTitle(settings.optString("title", ""))
|
||||||
.setContentText(settings.optString("text", ""))
|
.setContentText(settings.optString("text", ""))
|
||||||
.setTicker(settings.optString("ticker", ""))
|
|
||||||
.setOngoing(true)
|
.setOngoing(true)
|
||||||
.setSmallIcon(getIconResId());
|
.setSmallIcon(getIconResId());
|
||||||
|
|
||||||
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
||||||
if(settings.optBoolean("isPublic") == true) {
|
|
||||||
notification.setVisibility(Notification.VISIBILITY_PUBLIC);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!settings.optString("color").equals("")) {
|
|
||||||
try {
|
|
||||||
notification.setColor(Color.parseColor(settings.optString("color")));
|
|
||||||
} catch (Exception e) {
|
|
||||||
Log.e("BackgroundMode", settings.optString("color") + " is not a valid color");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (intent != null && settings.optBoolean("resume")) {
|
if (intent != null && settings.optBoolean("resume")) {
|
||||||
|
|
||||||
PendingIntent contentIntent = PendingIntent.getActivity(
|
PendingIntent contentIntent = PendingIntent.getActivity(
|
||||||
context, NOTIFICATION_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
context, NOTIFICATION_ID, intent,
|
||||||
|
PendingIntent.FLAG_UPDATE_CURRENT);
|
||||||
|
|
||||||
notification.setContentIntent(contentIntent);
|
notification.setContentIntent(contentIntent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT < 16) {
|
|
||||||
// Build notification for HoneyComb to ICS
|
|
||||||
return notification.getNotification();
|
|
||||||
} else {
|
|
||||||
// Notification for Jellybean and above
|
|
||||||
return notification.build();
|
return notification.build();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update the notification.
|
||||||
|
*/
|
||||||
public void updateNotification() {
|
public void updateNotification() {
|
||||||
Notification n = makeNotification();
|
Notification notification = makeNotification();
|
||||||
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
NotificationManager service = (NotificationManager)
|
||||||
mNotificationManager.notify(NOTIFICATION_ID, n);
|
getSystemService(Context.NOTIFICATION_SERVICE);
|
||||||
|
|
||||||
|
service.notify(NOTIFICATION_ID, notification);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -209,10 +176,9 @@ public class ForegroundService extends Service {
|
|||||||
Context context = getApplicationContext();
|
Context context = getApplicationContext();
|
||||||
Resources res = context.getResources();
|
Resources res = context.getResources();
|
||||||
String pkgName = context.getPackageName();
|
String pkgName = context.getPackageName();
|
||||||
|
String icon = settings.optString("icon", "icon");
|
||||||
|
|
||||||
int resId = res.getIdentifier(settings.optString("icon", "icon"), "drawable", pkgName);
|
return res.getIdentifier(icon, "drawable", pkgName);
|
||||||
|
|
||||||
return resId;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -30,11 +30,6 @@ channel.onCordovaReady.subscribe(function () {
|
|||||||
// Set the defaults
|
// Set the defaults
|
||||||
exports.setDefaults({});
|
exports.setDefaults({});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Only enable WP8 by default
|
|
||||||
if (['WinCE', 'Win32NT'].indexOf(device.platform) > -1) {
|
|
||||||
exports.enable();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
@ -60,11 +55,8 @@ exports._isActive = false;
|
|||||||
exports._defaults = {
|
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',
|
|
||||||
resume: true,
|
resume: true,
|
||||||
silent: false,
|
silent: false,
|
||||||
isPublic: false,
|
|
||||||
color: "",
|
|
||||||
icon: "icon"
|
icon: "icon"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user