Search code examples
androidmultithreadingasynchronousandroid-asynctaskalarmmanager

From where to launch AlarmManager?


I try to make an application to periodically report to a server, and I want this start since the application is installed. My question is from where do I launch the AlarmManager?


Solution

  • If you want to set the alarm as soon as your application starts, you can do it in your Application class. For that create a class let's say MyApplication which extends Application class and in its onCreate() method, set your alarm. onCreate() will invoke as soon as you launch a new instance of your application.

    public class MyApplication extends Application {
    
       @Override
        public void onCreate() {
          //Set Alarm here
       }
    }
    

    And to tell Android runtime that MyApplication is your application class, change your application's manifest file :

    <application
            android:name="<your_package_name>.MyApplication"
    ....
    >