Browse Source

Reschedule alarms on boot

Thomas Dy 7 years ago
parent
commit
5751c583f3

+ 7 - 0
src/main/AndroidManifest.xml

@@ -4,6 +4,7 @@
       android:versionCode="1"
       android:versionName="1.0">
 
+    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
     <uses-permission android:name="android.permission.SEND_SMS" />
 
     <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
@@ -15,5 +16,11 @@
             </intent-filter>
         </activity>
         <service android:name="SendService"></service>
+        <receiver android:name="BootReceiver" android:enabled="true" android:exported="true">
+            <intent-filter>
+                <action android:name="android.intent.action.BOOT_COMPLETED" />
+                <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter>
+        </receiver>
     </application>
 </manifest>

+ 13 - 0
src/main/kotlin/BootReceiver.kt

@@ -0,0 +1,13 @@
+package com.pleasantprogrammer.mpp
+
+import android.content.BroadcastReceiver
+import android.content.Context
+import android.content.Intent
+
+class BootReceiver : BroadcastReceiver() {
+
+  override fun onReceive(context: Context, intent: Intent) {
+    context.startService(SendService.makeBootIntent(context))
+  }
+
+}

+ 1 - 1
src/main/kotlin/MainActivity.kt

@@ -20,7 +20,7 @@ class MainActivity : Activity() {
   }
 
   fun sendRequest() {
-    startService(SendService.makeIntent(this))
+    startService(SendService.makeSendIntent(this))
   }
 
 }

+ 13 - 2
src/main/kotlin/SendService.kt

@@ -13,14 +13,21 @@ class SendService : IntentService("SendService") {
 
   companion object {
     val ACTION_SEND = "com.pleasantprogrammer.mpp.action.SEND"
+    val ACTION_BOOT = "com.pleasantprogrammer.mpp.action.BOOT"
 
     val INTERVAL = 60 * 1000L
 
-    fun makeIntent(ctx: Context): Intent {
+    fun makeSendIntent(ctx: Context): Intent {
       val intent = Intent(ACTION_SEND)
       intent.setClass(ctx, SendService::class.java)
       return intent
     }
+
+    fun makeBootIntent(ctx: Context): Intent {
+      val intent = Intent(ACTION_BOOT)
+      intent.setClass(ctx, SendService::class.java)
+      return intent
+    }
   }
 
   val sharedPreferences: SharedPreferences by lazy {
@@ -35,6 +42,10 @@ class SendService : IntentService("SendService") {
       sharedPreferences.edit().putLong("last_requested", System.currentTimeMillis()).commit()
       scheduleNext()
     }
+    else if(intent.action == ACTION_BOOT) {
+      Log.i("SendService", "Rescheduling because of restart")
+      scheduleNext()
+    }
   }
 
   fun scheduleNext() {
@@ -42,7 +53,7 @@ class SendService : IntentService("SendService") {
     if(from < 0) return
 
     val pendingIntent = PendingIntent.getService(
-        this, 0, makeIntent(this), PendingIntent.FLAG_ONE_SHOT
+        this, 0, makeSendIntent(this), PendingIntent.FLAG_ONE_SHOT
     )
 
     val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager