From 57e6c537d01231c74c26e0cd27107330305391e6 Mon Sep 17 00:00:00 2001 From: Tal Regev Date: Thu, 10 Jun 2021 00:07:49 +0300 Subject: [PATCH] Run boinc after boot Fix for android 8 and above --- .../BOINC/app/src/main/AndroidManifest.xml | 13 ++++++++---- .../berkeley/boinc/receiver/BOINCReceivers.kt | 20 +++++++++++++++---- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/android/BOINC/app/src/main/AndroidManifest.xml b/android/BOINC/app/src/main/AndroidManifest.xml index 81b5ec765a..1f147327ef 100644 --- a/android/BOINC/app/src/main/AndroidManifest.xml +++ b/android/BOINC/app/src/main/AndroidManifest.xml @@ -27,8 +27,9 @@ android:normalScreens="true" android:smallScreens="true" android:xlargeScreens="true" /> - + + @@ -122,20 +123,24 @@ android:process=":remote" /> - + + + - + + - + diff --git a/android/BOINC/app/src/main/java/edu/berkeley/boinc/receiver/BOINCReceivers.kt b/android/BOINC/app/src/main/java/edu/berkeley/boinc/receiver/BOINCReceivers.kt index 188b3c7322..baff6a47be 100644 --- a/android/BOINC/app/src/main/java/edu/berkeley/boinc/receiver/BOINCReceivers.kt +++ b/android/BOINC/app/src/main/java/edu/berkeley/boinc/receiver/BOINCReceivers.kt @@ -21,6 +21,7 @@ package edu.berkeley.boinc.receiver import android.content.BroadcastReceiver import android.content.Context import android.content.Intent +import android.os.Build import android.util.Log import edu.berkeley.boinc.client.AppPreferences import edu.berkeley.boinc.client.Monitor @@ -33,7 +34,11 @@ class BootReceiver : BroadcastReceiver() { if (prefs.autostart) { Log.d(Logging.TAG, "BootReceiver autostart enabled, start Monitor...") - context.startService(Intent(context, Monitor::class.java)) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + context.startForegroundService(Intent(context, Monitor::class.java)) + } else { + context.startService(Intent(context, Monitor::class.java)) + } } else { // do nothing Log.d(Logging.TAG, "BootReceiver autostart disabled - do nothing") @@ -53,7 +58,11 @@ class PackageReplacedReceiver : BroadcastReceiver() { if (intent.action == Intent.ACTION_PACKAGE_REPLACED) { if (intent.dataString.toString().contains("edu.berkeley.boinc")) { Log.d(Logging.TAG, "PackageReplacedReceiver: starting service...") - context.startService(Intent(context, Monitor::class.java)) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + context.startForegroundService(Intent(context, Monitor::class.java)) + } else { + context.startService(Intent(context, Monitor::class.java)) + } } else { Log.d(Logging.TAG, "PackageReplacedReceiver: other package: " + intent.dataString) } @@ -65,8 +74,11 @@ class PowerConnectedReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { if (intent.action == Intent.ACTION_POWER_CONNECTED) { Log.d(Logging.TAG, "power connected, start service...") - - context.startService(Intent(context, Monitor::class.java)) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + context.startForegroundService(Intent(context, Monitor::class.java)) + } else { + context.startService(Intent(context, Monitor::class.java)) + } } } }