-
Notifications
You must be signed in to change notification settings - Fork 166
/
BootReceiver.java
32 lines (26 loc) · 1.12 KB
/
BootReceiver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package id.flutter.flutter_background_service;
import android.annotation.SuppressLint;
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 {
@SuppressLint("WakelockTimeout")
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences pref = context.getSharedPreferences("id.flutter.background_service", MODE_PRIVATE);
boolean autoStart = pref.getBoolean("auto_start_on_boot",true);
if(autoStart) {
if (BackgroundService.lockStatic == null){
BackgroundService.getLock(context).acquire();
}
if (BackgroundService.isForegroundService(context)) {
ContextCompat.startForegroundService(context, new Intent(context, BackgroundService.class));
} else {
context.startService(new Intent(context, BackgroundService.class));
}
}
}
}