Created
December 29, 2020 11:01
-
-
Save chydee/b4a0ae0b2b7ce225deb7386c932f429a to your computer and use it in GitHub Desktop.
Create Periodic Tasks With AlarmManager and JobIntentService
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.BroadcastReceiver | |
import android.content.Context | |
import android.content.Intent | |
import com.chydee.notekeeper.data.DBHelper | |
import com.chydee.notekeeper.data.DBHelperImpl | |
import io.reactivex.android.schedulers.AndroidSchedulers | |
import io.reactivex.disposables.CompositeDisposable | |
import io.reactivex.schedulers.Schedulers | |
class AlarmReceiver : BroadcastReceiver() { | |
companion object { | |
const val REQUEST_CODE = 2021 | |
} | |
private lateinit var dbHelper: DBHelper | |
private val compositeDisposable = CompositeDisposable() | |
// Triggered by the Alarm periodically (starts the service to run task) | |
override fun onReceive(context: Context?, intent: Intent?) { | |
val i = Intent(context, TrashService::class.java) | |
TrashService().enqueueWork(context, i) | |
if (context != null) { | |
// Or Do anything here | |
dbHelper = DBHelperImpl(context) | |
dbHelper.clearTrash().subscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe({}, {}).let { compositeDisposable.add(it) } | |
} | |
context?.startService(i) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<application> | |
... | |
<receiver | |
android:name=".alarm.TrashAlarmReceiver" | |
android:process=":remote" /> | |
<service | |
android:name=".alarm.TrashService" | |
android:enabled="true" | |
android:exported="true" | |
android:permission="android.permission.BIND_JOB_SERVICE" /> | |
</application> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context | |
import android.content.Intent | |
import androidx.core.app.JobIntentService | |
import timber.log.Timber | |
class ExampleService : JobIntentService() { | |
fun enqueueWork(context: Context?, intent: Intent) { | |
if (context != null) { | |
enqueueWork(context, TrashService::class.java, Companion.JOB_ID, intent) | |
} | |
} | |
override fun onCreate() { | |
super.onCreate() | |
Timber.d("Service onCreate") | |
} | |
override fun onHandleWork(intent: Intent) { | |
// Do the task here | |
Timber.i("Handle Work"); | |
} | |
companion object { | |
/** | |
* Unique job ID for this service. | |
*/ | |
const val JOB_ID = 2020 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void scheduleClearTrashAlarm() { | |
// Construct an intent that will execute the AlarmReceiver | |
Timber.tag("MyTestService").d("Alarm scheduleing"); | |
Intent intent = new Intent(this, TrashAlarmReceiver.class); | |
// Create a PendingIntent to be triggered when the alarm goes off | |
final PendingIntent pIntent = PendingIntent.getBroadcast(this, TrashAlarmReceiver.REQUEST_CODE, | |
intent, PendingIntent.FLAG_UPDATE_CURRENT); | |
// Setup periodic alarm every every half hour from this point onwards | |
long firstMillis = System.currentTimeMillis(); // alarm is set right away | |
AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); | |
// First parameter is the type: ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP, RTC_WAKEUP | |
// Interval can be INTERVAL_FIFTEEN_MINUTES, INTERVAL_HALF_HOUR, INTERVAL_HOUR, INTERVAL_DAY | |
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis, 60 * 1000 | |
/*(AlarmManager.INTERVAL_DAY * 7)*/, pIntent); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment