zubairehman / AlarmManager

A simple library written in Kotlin that allows you to set an alarm and get notified really easily.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AlarmManager

AlarmManager provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future. When an alarm goes off, the Intent that had been registered for it is broadcast by the system that you can receive inside your activity. Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted. Alarm has been set automatically on boot completed.

How to use

Step 1. Add the JitPack repository to your build file. Add it in your root build.gradle at the end of repositories:

allprojects {
    repositories {
    ...
    maven { url 'https://jitpack.io' }
    }
}

Step 2. Add the dependency

dependencies {
    implementation 'com.github.zubairehman:AlarmManager:v1.0.0-alpha01'
}

Using AlarmManager is really simple and easy, all you need to do is to pass in your configuration and start getting notifications in your activity/fragment. This could further be illustrated in the example below:

AlarmBuilder().with(context)
              .setTimeInMilliSeconds(TimeUnit.SECONDS.toMillis(10))
              .setId("UPDATE_INFO_SYSTEM_SERVICE")
              .setAlarmType(AlarmType.REPEAT)
              .setAlarm()

Note that you can get a builder object for later use, as shown in the code below:

val builder = AlarmBuilder().with(context)
        .setTimeInMilliSeconds(TimeUnit.SECONDS.toMillis(10))
        .setId("UPDATE_INFO_SYSTEM_SERVICE")
        .setAlarmType(AlarmType.REPEAT)
        
builder?.setAlarm()

Register listener

In-order to get notified, you need to register a listener in onResume as shown below:

override fun onResume() {
    super.onResume()
    builder?.addListener(this)
}

Un-Register listener

In-order to prevent your activity listening to un-attended events you need to un-register your listener in onPause as shown below:

override fun onPause() {
    super.onPause()
    builder?.removeListener(this)
}

Cancel alarm

In-order to cancel your alarm just call cancelAlarm() as shown below:

builder?.cancelAlarm()

Callback

The perform() method will be called once the desired time has been reached, you can write your logic in this method as shown below:

override fun perform(context: Context, intent: Intent) {
    Timber.i("Do your work here")
}

Complete Example:

Here is the complete code that demonstrate the use of AlarmManager:

class MainActivity : AppCompatActivity(), AlarmListener {

    //Alarm builder
    var builder: AlarmBuilder? = null;

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //creating alarm builder
        builder = AlarmBuilder().with(this)
                .setTimeInMilliSeconds(TimeUnit.SECONDS.toMillis(10))
                .setId("UPDATE_INFO_SYSTEM_SERVICE")
                .setAlarmType(AlarmType.REPEAT)

        //setting click listeners
        btnSetAlarm.setOnClickListener {
            builder?.setAlarm()
        }

        btnCancelAlarm.setOnClickListener {
            builder?.cancelAlarm()
        }
    }

    override fun onResume() {
        super.onResume()
        builder?.addListener(this)
    }

    override fun onPause() {
        super.onPause()
        builder?.removeListener(this)
    }

    override fun perform(context: Context, intent: Intent) {
        Log.i("Alarm", "Do your work here")
    }
}

About

A simple library written in Kotlin that allows you to set an alarm and get notified really easily.

License:MIT License


Languages

Language:Kotlin 90.3%Language:Java 9.7%