|
@@ -1,6 +1,8 @@
|
|
package com.pleasantprogrammer.mpp;
|
|
package com.pleasantprogrammer.mpp;
|
|
|
|
|
|
|
|
+import android.app.AlarmManager
|
|
import android.app.IntentService
|
|
import android.app.IntentService
|
|
|
|
+import android.app.PendingIntent
|
|
import android.content.Context
|
|
import android.content.Context
|
|
import android.content.Intent
|
|
import android.content.Intent
|
|
import android.content.SharedPreferences
|
|
import android.content.SharedPreferences
|
|
@@ -10,6 +12,14 @@ class SendService : IntentService("SendService") {
|
|
|
|
|
|
companion object {
|
|
companion object {
|
|
val ACTION_SEND = "com.pleasantprogrammer.mpp.action.SEND"
|
|
val ACTION_SEND = "com.pleasantprogrammer.mpp.action.SEND"
|
|
|
|
+
|
|
|
|
+ val INTERVAL = 60 * 1000L
|
|
|
|
+
|
|
|
|
+ fun makeIntent(ctx: Context): Intent {
|
|
|
|
+ val intent = Intent(ACTION_SEND)
|
|
|
|
+ intent.setClass(ctx, SendService::class.java)
|
|
|
|
+ return intent
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
val sharedPreferences: SharedPreferences by lazy {
|
|
val sharedPreferences: SharedPreferences by lazy {
|
|
@@ -21,6 +31,20 @@ class SendService : IntentService("SendService") {
|
|
val smsManager = SmsManager.getDefault()
|
|
val smsManager = SmsManager.getDefault()
|
|
smsManager.sendTextMessage("5554", null, "GS99", null, null)
|
|
smsManager.sendTextMessage("5554", null, "GS99", null, null)
|
|
sharedPreferences.edit().putLong("last_requested", System.currentTimeMillis()).commit()
|
|
sharedPreferences.edit().putLong("last_requested", System.currentTimeMillis()).commit()
|
|
|
|
+ scheduleNext()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ fun scheduleNext() {
|
|
|
|
+ val from = sharedPreferences.getLong("last_requested", -1)
|
|
|
|
+ if(from < 0) return
|
|
|
|
+
|
|
|
|
+ val pendingIntent = PendingIntent.getService(
|
|
|
|
+ this, 0, makeIntent(this), PendingIntent.FLAG_ONE_SHOT
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+ val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
|
|
|
|
+ alarmManager.cancel(pendingIntent)
|
|
|
|
+ alarmManager.set(AlarmManager.RTC_WAKEUP, from + INTERVAL, pendingIntent)
|
|
|
|
+ }
|
|
}
|
|
}
|