Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue with forced restart #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions app/src/main/java/com/robertohuertas/endless/EndlessService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@ class EndlessService : Service() {
}

override fun onTaskRemoved(rootIntent: Intent) {
val restartServiceIntent = Intent(applicationContext, EndlessService::class.java).also {
it.setPackage(packageName)
};
val restartServicePendingIntent: PendingIntent = PendingIntent.getService(this, 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
applicationContext.getSystemService(Context.ALARM_SERVICE);
val alarmService: AlarmManager = applicationContext.getSystemService(Context.ALARM_SERVICE) as AlarmManager;
alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 1000, restartServicePendingIntent);
if (forceRestart) {
val restartServiceIntent = Intent(applicationContext, EndlessService::class.java).also {
it.setPackage(packageName)
};
val restartServicePendingIntent: PendingIntent = PendingIntent.getService(this, 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
applicationContext.getSystemService(Context.ALARM_SERVICE);
val alarmService: AlarmManager = applicationContext.getSystemService(Context.ALARM_SERVICE) as AlarmManager;
alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 1000, restartServicePendingIntent);
}
}

private fun startService() {
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/robertohuertas/endless/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ class MainActivity : AppCompatActivity() {
title = "Endless Service"

findViewById<Button>(R.id.btnStartService).let {
forceRestart = true
it.setOnClickListener {
log("START THE FOREGROUND SERVICE ON DEMAND")
actionOnService(Actions.START)
}
}

findViewById<Button>(R.id.btnStopService).let {
forceRestart = false
it.setOnClickListener {
log("STOP THE FOREGROUND SERVICE ON DEMAND")
actionOnService(Actions.STOP)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ enum class ServiceState {

private const val name = "SPYSERVICE_KEY"
private const val key = "SPYSERVICE_STATE"
var forceRestart: Boolean = true

fun setServiceState(context: Context, state: ServiceState) {
val sharedPrefs = getPreferences(context)
Expand All @@ -22,6 +23,9 @@ fun setServiceState(context: Context, state: ServiceState) {
fun getServiceState(context: Context): ServiceState {
val sharedPrefs = getPreferences(context)
val value = sharedPrefs.getString(key, ServiceState.STOPPED.name)
if (value == ServiceState.STOPPED.name) {
forceRestart = false
}
return ServiceState.valueOf(value)
}

Expand Down