Skip to content

Commit

Permalink
Merge pull request ekasetiawans#9 from canewsin/auto-start-pref
Browse files Browse the repository at this point in the history
Added Pref for Auto Start Service on Boot
  • Loading branch information
ekasetiawans authored Oct 25, 2020
2 parents 702a768 + 290bf73 commit 6ba79ec
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,23 @@ public static void enqueue(Context context){
AlarmManagerCompat.setAndAllowWhileIdle(manager, AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, pIntent);
}

public static void setCallbackDispatcher(Context context, long callbackHandleId, boolean isForeground){
public static void setCallbackDispatcher(Context context, long callbackHandleId, boolean isForeground, boolean autoStartOnBoot){
SharedPreferences pref = context.getSharedPreferences("id.flutter.background_service", MODE_PRIVATE);
pref.edit().putLong("callback_handle", callbackHandleId).putBoolean("is_foreground", isForeground).apply();
pref.edit()
.putLong("callback_handle", callbackHandleId)
.putBoolean("is_foreground", isForeground)
.putBoolean("auto_start_on_boot", autoStartOnBoot)
.apply();
}

public void setAutoStartOnBootMode(boolean value){
SharedPreferences pref = getSharedPreferences("id.flutter.background_service", MODE_PRIVATE);
pref.edit().putBoolean("auto_start_on_boot", value).apply();
}

public static boolean isAutoStartOnBootMode(Context context){
SharedPreferences pref = context.getSharedPreferences("id.flutter.background_service", MODE_PRIVATE);
return pref.getBoolean("auto_start_on_boot", true);
}

public void setForegroundServiceMode(boolean value){
Expand Down Expand Up @@ -179,6 +193,14 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
return;
}
}

if (method.equalsIgnoreCase("setAutoStartOnBootMode")){
JSONObject arg = (JSONObject) call.arguments;
boolean value = arg.getBoolean("value");
setAutoStartOnBootMode(value);
result.success(true);
return;
}

if (method.equalsIgnoreCase("setForegroundMode")){
JSONObject arg = (JSONObject) call.arguments;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

import androidx.core.content.ContextCompat;

import static android.content.Context.MODE_PRIVATE;


public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (BackgroundService.isForegroundService(context)){
ContextCompat.startForegroundService(context, new Intent(context, BackgroundService.class));
} else {
context.startService(new Intent(context, BackgroundService.class));
SharedPreferences pref = context.getSharedPreferences("id.flutter.background_service", MODE_PRIVATE);
boolean autoStart = pref.getBoolean("auto_start_on_boot",true);
if(autoStart) {
if (BackgroundService.isForegroundService(context)){
ContextCompat.startForegroundService(context, new Intent(context, BackgroundService.class));
} else {
context.startService(new Intent(context, BackgroundService.class));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
if ("BackgroundService.start".equals(method)) {
long callbackHandle = arg.getLong("handle");
boolean isForeground = arg.getBoolean("is_foreground_mode");
boolean autoStartOnBoot = arg.getBoolean("auto_start_on_boot");

BackgroundService.setCallbackDispatcher(context, callbackHandle, isForeground);
BackgroundService.setCallbackDispatcher(context, callbackHandle, isForeground, autoStartOnBoot);
BackgroundService.enqueue(context);

Intent intent = new Intent(context, BackgroundService.class);
Expand Down
9 changes: 9 additions & 0 deletions lib/flutter_background_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class FlutterBackgroundService {
static Future<bool> initialize(
Function onStart, {
bool foreground = true,
bool autoStart = true,
}) async {
final CallbackHandle handle = PluginUtilities.getCallbackHandle(onStart);
if (handle == null) {
Expand All @@ -54,6 +55,7 @@ class FlutterBackgroundService {
{
"handle": handle.toRawHandle(),
"is_foreground_mode": foreground,
"auto_start_on_boot": autoStart,
},
);

Expand Down Expand Up @@ -89,6 +91,13 @@ class FlutterBackgroundService {
});
}

void setAutoStartOnBootMode(bool value) {
if (Platform.isAndroid)
_backgroundChannel.invokeMethod("setAutoStartOnBootMode", {
"value": value,
});
}

StreamController<Map<String, dynamic>> _streamController =
StreamController.broadcast();

Expand Down

0 comments on commit 6ba79ec

Please sign in to comment.