"Fix" notification and foreground service on Oreo

This commit is contained in:
Tad 2018-04-05 21:08:52 -04:00
parent fa9e4c0b15
commit 0eb386f8ce
5 changed files with 23 additions and 4 deletions

View file

@ -6,8 +6,8 @@ android {
applicationId "us.spotco.malwarescanner"
minSdkVersion 16
targetSdkVersion 26
versionCode 29
versionName "2.4"
versionCode 30
versionName "2.5"
}
buildTypes {
debug {

View file

@ -134,8 +134,8 @@ public class MainActivity extends AppCompatActivity {
case R.id.toggleRealtime:
Intent realtimeScanner = new Intent(getApplicationContext(), MalwareScannerService.class);
if (!item.isChecked()) {
startService(realtimeScanner);
prefs.edit().putBoolean("autostart", true).apply();
Utils.considerStartService(this);
} else {
stopService(realtimeScanner);
prefs.edit().putBoolean("autostart", false).apply();

View file

@ -1,10 +1,12 @@
package us.spotco.malwarescanner;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Environment;
import android.os.FileObserver;
import android.os.IBinder;
@ -93,6 +95,16 @@ public class MalwareScannerService extends Service {
.setPriority(Notification.PRIORITY_MIN)
.setShowWhen(false);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel foregroundChannel = new NotificationChannel("FOREGROUND", getString(R.string.lblNotificationRealtimeTitle), NotificationManager.IMPORTANCE_LOW);
foregroundChannel.setShowBadge(false);
foregroundChannel.setDescription(getString(R.string.lblNotificationRealtimeDescription));
notificationManager.createNotificationChannel(foregroundChannel);
foregroundNotification.setChannelId("FOREGROUND");
//Importance seems to be getting ignored
//Users will have to manually set it to 'Low' in order to hide it from the lock screen and status bar
}
startForeground(-1, foregroundNotification.build());
}

View file

@ -4,6 +4,7 @@ import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import java.io.File;
import java.util.HashSet;
@ -74,9 +75,13 @@ class Utils {
if (autostart) {
Intent realtimeScanner = new Intent(context, MalwareScannerService.class);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(realtimeScanner);
} else {
context.startService(realtimeScanner);
}
}
}
}
}

View file

@ -8,7 +8,9 @@
<string name="lblScanInternal">Scan Internal Storage</string>
<string name="lblScanExternal">Scan External Storage</string>
<string name="lblNotificationRealtimeTitle">Realtime Scanner</string>
<string name="lblNotificationRealtimeDescription">Used to show files scanned counter and maintain the background service</string>
<string name="lblNotificationRealtimeText">Malware will be detected in realtime</string>
<string name="lblNotificationRealtimeDetection">Malware Detected:</string>
<string name="lblRealtimeScannerToggle">Realtime Scanner</string>
</resources>