mirror of
https://bitbucket.org/TheBosZ/cordova-plugin-run-in-background
synced 2024-11-14 19:44:53 +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"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
id="cordova-plugin-background-mode"
|
||||
version="0.6.6.dev">
|
||||
version="0.6.6-dev">
|
||||
|
||||
<name>BackgroundMode</name>
|
||||
|
||||
@ -72,7 +72,10 @@
|
||||
* candidate for killing when low on memory.
|
||||
-->
|
||||
<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>
|
||||
|
||||
<source-file
|
||||
|
@ -21,12 +21,6 @@
|
||||
|
||||
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.content.ComponentName;
|
||||
import android.content.Context;
|
||||
@ -34,6 +28,12 @@ import android.content.Intent;
|
||||
import android.content.ServiceConnection;
|
||||
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 {
|
||||
|
||||
// Event types for callbacks
|
||||
@ -230,15 +230,15 @@ public class BackgroundMode extends CordovaPlugin {
|
||||
private void startService() {
|
||||
Activity context = cordova.getActivity();
|
||||
|
||||
Intent intent = new Intent(
|
||||
context, ForegroundService.class);
|
||||
|
||||
if (isDisabled || isBind)
|
||||
return;
|
||||
|
||||
Intent intent = new Intent(
|
||||
context, ForegroundService.class);
|
||||
|
||||
try {
|
||||
context.bindService(
|
||||
intent, connection, Context.BIND_AUTO_CREATE);
|
||||
context.bindService(intent,
|
||||
connection, Context.BIND_AUTO_CREATE);
|
||||
|
||||
fireEvent(Event.ACTIVATE, null);
|
||||
|
||||
@ -282,9 +282,6 @@ public class BackgroundMode extends CordovaPlugin {
|
||||
private void fireEvent (Event event, String params) {
|
||||
String eventName;
|
||||
|
||||
if (updateSettings != null && event != Event.FAILURE)
|
||||
return;
|
||||
|
||||
switch (event) {
|
||||
case ACTIVATE:
|
||||
eventName = "activate"; break;
|
||||
|
@ -21,12 +21,6 @@
|
||||
|
||||
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.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
@ -34,13 +28,13 @@ import android.app.Service;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.Color;
|
||||
import android.os.Binder;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.os.IBinder;
|
||||
import android.os.PowerManager;
|
||||
import android.util.Log;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
/**
|
||||
* 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
|
||||
@ -51,16 +45,10 @@ public class ForegroundService extends Service {
|
||||
// Fixed ID for the 'foreground' notification
|
||||
private static final int NOTIFICATION_ID = -574543954;
|
||||
|
||||
private Notification.Builder notification;
|
||||
|
||||
// Binder given to clients
|
||||
private final IBinder mBinder = new ForegroundBinder();
|
||||
|
||||
// Scheduler to exec periodic tasks
|
||||
final Timer scheduler = new Timer();
|
||||
|
||||
// Used to keep the app alive
|
||||
TimerTask keepAliveTask;
|
||||
private PowerManager.WakeLock wakeLock;
|
||||
|
||||
/**
|
||||
* Allow clients to call on to the service.
|
||||
@ -91,6 +79,9 @@ public class ForegroundService extends Service {
|
||||
keepAwake();
|
||||
}
|
||||
|
||||
/**
|
||||
* No need to run headless on destroy.
|
||||
*/
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
@ -102,7 +93,6 @@ public class ForegroundService extends Service {
|
||||
* by the OS.
|
||||
*/
|
||||
public void keepAwake() {
|
||||
final Handler handler = new Handler();
|
||||
|
||||
if (!this.inSilentMode()) {
|
||||
startForeground(NOTIFICATION_ID, makeNotification());
|
||||
@ -112,20 +102,12 @@ public class ForegroundService extends Service {
|
||||
|
||||
BackgroundMode.deleteUpdateSettings();
|
||||
|
||||
keepAliveTask = new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
handler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// Nothing to do here
|
||||
// Log.d("BackgroundMode", "" + new Date().getTime());
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
PowerManager powerMgr = (PowerManager) getSystemService(POWER_SERVICE);
|
||||
|
||||
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() {
|
||||
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
|
||||
* main activity.
|
||||
*/
|
||||
@SuppressLint("NewApi")
|
||||
@SuppressWarnings("deprecation")
|
||||
private Notification makeNotification() {
|
||||
JSONObject settings = BackgroundMode.getSettings();
|
||||
Context context = getApplicationContext();
|
||||
@ -153,49 +137,32 @@ public class ForegroundService extends Service {
|
||||
Intent intent = context.getPackageManager()
|
||||
.getLaunchIntentForPackage(pkgName);
|
||||
|
||||
notification = new Notification.Builder(context)
|
||||
.setContentTitle(settings.optString("title", ""))
|
||||
.setContentText(settings.optString("text", ""))
|
||||
.setTicker(settings.optString("ticker", ""))
|
||||
.setOngoing(true)
|
||||
.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");
|
||||
}
|
||||
}
|
||||
}
|
||||
Notification.Builder notification = new Notification.Builder(context)
|
||||
.setContentTitle(settings.optString("title", ""))
|
||||
.setContentText(settings.optString("text", ""))
|
||||
.setOngoing(true)
|
||||
.setSmallIcon(getIconResId());
|
||||
|
||||
if (intent != null && settings.optBoolean("resume")) {
|
||||
|
||||
PendingIntent contentIntent = PendingIntent.getActivity(
|
||||
context, NOTIFICATION_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
context, NOTIFICATION_ID, intent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
|
||||
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() {
|
||||
Notification n = makeNotification();
|
||||
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
mNotificationManager.notify(NOTIFICATION_ID, n);
|
||||
Notification notification = makeNotification();
|
||||
NotificationManager service = (NotificationManager)
|
||||
getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
|
||||
service.notify(NOTIFICATION_ID, notification);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -209,10 +176,9 @@ public class ForegroundService extends Service {
|
||||
Context context = getApplicationContext();
|
||||
Resources res = context.getResources();
|
||||
String pkgName = context.getPackageName();
|
||||
String icon = settings.optString("icon", "icon");
|
||||
|
||||
int resId = res.getIdentifier(settings.optString("icon", "icon"), "drawable", pkgName);
|
||||
|
||||
return resId;
|
||||
return res.getIdentifier(icon, "drawable", pkgName);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -30,11 +30,6 @@ channel.onCordovaReady.subscribe(function () {
|
||||
// Set the defaults
|
||||
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 = {
|
||||
title: 'App is running in background',
|
||||
text: 'Doing heavy tasks.',
|
||||
ticker: 'App is running in background',
|
||||
resume: true,
|
||||
silent: false,
|
||||
isPublic: false,
|
||||
color: "",
|
||||
icon: "icon"
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user