Lucas Doineau
Lucas Doineau

Reputation: 1

Restart my app after an hour in the background

I want to restart my application when the user has the app in the background after one hour. I already did some research and I use a Service:

public class BackgroundService extends Service {

    private static final long CHECK_INTERVAL = 60 * 60 * 1000; 
    private Handler handler;
    private long lastForegroundTime = 0;

    @Override
    public void onCreate() {
        super.onCreate();
        handler = new Handler();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                
                if (isAppInForeground()) {
                    
                    lastForegroundTime = System.currentTimeMillis();
                    handler.postDelayed(this, CHECK_INTERVAL);
                } else {
                    
                    long elapsedTime = System.currentTimeMillis() - lastForegroundTime;
                    if (elapsedTime > CHECK_INTERVAL) {
                        Intent restartIntent = new Intent(BackgroundService.this, MainActivity.class);
                        restartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(restartIntent);
                    } else {
                        handler.postDelayed(this, CHECK_INTERVAL - elapsedTime);
                    }
                }
            }
        }, CHECK_INTERVAL);

        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        
        return null;
    }

    private boolean isAppInForeground() {
        ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        if (activityManager != null) {
            
            List<ActivityManager.RunningAppProcessInfo> processes = activityManager.getRunningAppProcesses();
            if (processes != null) {
                for (ActivityManager.RunningAppProcessInfo process : processes) {
                    if (process.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                        for (String packageName : process.pkgList) {
                            if (packageName.equals(getPackageName())) {
                                return true;
                            }
                        }
                    }
                }
            }
        }
        return false;
    }
}

But I know the service is not the best option for the battery, and there are still problems in my Service. Are there any other options to do the same thing? I heard we can use AlarmManager...

I want to restart my app after one hour in the background.

Upvotes: 0

Views: 94

Answers (1)

Vincent kbkop
Vincent kbkop

Reputation: 21

Maybe can try steps below, hope it helps:

  1. Create a Service: Override the onStartCommand() method where you put the logic you want to execute which i believe you have already done that.

  2. Set up AlarmManager during your app start up: Set up the AlarmManager to trigger your Service periodically or any adhoc time. You will use AlarmManager to schedule your service to run at specific intervals.

eg.

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, MyBackgroundService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);

// Schedule the alarm to start in one hour, and then repeat every hour
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + AlarmManager.INTERVAL_HOUR, AlarmManager.INTERVAL_HOUR, pendingIntent);
  1. Register the service and permission in android manifest:

<service android:name=".MyBackgroundService"/>
......
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
......

Upvotes: 0

Related Questions