Search code examples
javaandroidservicealarmmanager

Android: how to intercept when a Service runs for the first time


Hi and thanks for your help.

I have the following situation.

I have an AlarmManager that fires off a Service every 1 minute.

I have to execute a specific method in that Service only when the Service is started for the first time. not when it starts the subsequent times.

I do not think shared preferences is a solutions, because they are persisted if the phone is switched off.

Please how do I solve this ??

Thank you for any suggestion!!!


Solution

  • Try inheriting form the App and store in it..

    public class YourApp extends Application {
    
    private static boolean mFirstRun = false;
    
    public static boolean getFirstRun() { return mFirstRun; }
    public static void clearFirstRun() { mFirstRun = false; }
    

    from your service:

    if (YourApp.getFirstRun()) 
    {
       clearFirstRun();
       // run first time code
    }