Skip to content

Commit e822eb3

Browse files
committed
Updates for SDK >= 31
1 parent a3adddb commit e822eb3

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/android/BackgroundMode.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ Licensed to the Apache Software Foundation (ASF) under one
1818
specific language governing permissions and limitations
1919
under the License.
2020
*/
21-
2221
package de.appplant.cordova.plugin.background;
2322

2423
import android.app.Activity;
@@ -58,7 +57,7 @@ private enum Event { ACTIVATE, DEACTIVATE, FAILURE }
5857
private static JSONObject defaultSettings = new JSONObject();
5958

6059
// Service that keeps the app awake
61-
private ForegroundService service;
60+
private ForegroundService service = null;
6261

6362
// Used to (un)bind the service to with the activity
6463
private final ServiceConnection connection = new ServiceConnection()
@@ -223,7 +222,7 @@ static JSONObject getSettings () {
223222
*/
224223
private void updateNotification(JSONObject settings)
225224
{
226-
if (isBind) {
225+
if (isBind && service != null) {
227226
service.updateNotification(settings);
228227
}
229228
}

src/android/BackgroundModeExt.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,18 +155,27 @@ else if(1 == wakeCounter) {
155155
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
156156

157157
Intent newIntent = new Intent(RECEIVER);
158+
int flags = 0;
159+
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
160+
flags = flags | PendingIntent.FLAG_IMMUTABLE;
161+
}
158162

159-
alarmIntent = PendingIntent.getBroadcast(context, 0, newIntent, 0);
163+
alarmIntent = PendingIntent.getBroadcast(context, 0, newIntent, flags);
160164
alarmMgr.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP,
161165
SystemClock.elapsedRealtime() + timeout, alarmIntent);
162166
}
163167
catch(Exception e) {
168+
int flags = 0;
169+
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
170+
flags = flags | PendingIntent.FLAG_IMMUTABLE;
171+
}
172+
164173
Log.d("MlesTalk", "Got exception, no intent loaded, loading 60 s!");
165174
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
166175

167176
Intent newIntent = new Intent(RECEIVER);
168177

169-
alarmIntent = PendingIntent.getBroadcast(context, 0, newIntent, 0);
178+
alarmIntent = PendingIntent.getBroadcast(context, 0, newIntent, flags);
170179
alarmMgr.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP,
171180
SystemClock.elapsedRealtime() + 60*1000, alarmIntent);
172181
}
@@ -289,8 +298,12 @@ private void disableWebViewOptimizations() {
289298
context.registerReceiver(mReceiver, intentFilter);
290299

291300
Intent intent = new Intent(RECEIVER);
301+
int flags = 0;
302+
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
303+
flags = flags | PendingIntent.FLAG_IMMUTABLE;
304+
}
292305

293-
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
306+
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, flags);
294307
alarmMgr.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP,
295308
SystemClock.elapsedRealtime() + TIMEOUT, alarmIntent);
296309

0 commit comments

Comments
 (0)