Skip to content

Commit

Permalink
perf: improve android background service
Browse files Browse the repository at this point in the history
  • Loading branch information
ekasetiawans committed Sep 13, 2022
1 parent 4cf3f70 commit e01a3fa
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
Expand Up @@ -11,6 +11,7 @@
android:enabled="true"
android:exported="true"
android:name=".BackgroundService"
android:stopWithTask="false"
/>

<receiver
Expand Down
Expand Up @@ -2,6 +2,7 @@

import static android.os.Build.VERSION.SDK_INT;

import android.annotation.SuppressLint;
import android.app.AlarmManager;
import android.app.NotificationChannel;
import android.app.NotificationManager;
Expand All @@ -14,6 +15,7 @@
import android.os.IBinder;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.os.SystemClock;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.core.app.AlarmManagerCompat;
Expand Down Expand Up @@ -55,11 +57,12 @@ synchronized public static PowerManager.WakeLock getLock(Context context) {
if (lockStatic == null) {
PowerManager mgr = (PowerManager) context
.getSystemService(Context.POWER_SERVICE);
lockStatic = mgr.newWakeLock(PowerManager.FULL_WAKE_LOCK,
lockStatic = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
LOCK_NAME);
lockStatic.setReferenceCounted(true);
}
return (lockStatic);

return lockStatic;
}

@Override
Expand Down Expand Up @@ -201,15 +204,14 @@ public int onStartCommand(Intent intent, int flags, int startId) {

AtomicBoolean isRunning = new AtomicBoolean(false);

@SuppressLint("WakelockTimeout")
private void runService() {
try {
Log.d(TAG, "runService");
if (isRunning.get() || (backgroundEngine != null && !backgroundEngine.getDartExecutor().isExecutingDart()))
return;

if (lockStatic == null){
getLock(getApplicationContext()).acquire(10*60*1000L /*10 minutes*/);
}
getLock(getApplicationContext()).acquire();

updateNotificationInfo();

Expand Down Expand Up @@ -249,6 +251,22 @@ public void receiveData(JSONObject data) {
}
}

@Override
public void onTaskRemoved(Intent rootIntent) {

/// Restart service when user swipe the application from Recent Task
Intent restartServiceIntent = new Intent(getApplicationContext(), BackgroundService.class);
restartServiceIntent.setPackage(getPackageName());

int flags = PendingIntent.FLAG_ONE_SHOT;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
flags |= PendingIntent.FLAG_MUTABLE;
}
PendingIntent pi = PendingIntent.getService(this, 1, restartServiceIntent, flags);
AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 1000, pi);
}

@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
String method = call.method;
Expand All @@ -258,11 +276,6 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
SharedPreferences pref = getSharedPreferences("id.flutter.background_service", MODE_PRIVATE);
long backgroundHandle = pref.getLong("background_handle", 0);
result.success(backgroundHandle);

if (lockStatic != null) {
lockStatic.release();
lockStatic = null;
}
return;
}

Expand Down
@@ -1,5 +1,6 @@
package id.flutter.flutter_background_service;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
Expand All @@ -11,13 +12,14 @@


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(10*60*1000L /*10 minutes*/);
BackgroundService.getLock(context).acquire();
}

if (BackgroundService.isForegroundService(context)) {
Expand Down

0 comments on commit e01a3fa

Please sign in to comment.