Simplify restartService
This commit is contained in:
parent
9db96919bd
commit
342f701afd
@ -1,10 +1,7 @@
|
||||
package org.unifiedpush.distributor.nextpush.activities
|
||||
|
||||
import android.Manifest
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
@ -161,16 +158,9 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
private fun restart() {
|
||||
Log.d(TAG, "Restarting the Listener")
|
||||
val receiver = object : BroadcastReceiver() {
|
||||
override fun onReceive(p0: Context?, p1: Intent?) {
|
||||
RestartWorker.start(this@MainActivity, delay = 0)
|
||||
}
|
||||
StartService.service?.stopService {
|
||||
RestartWorker.start(this, delay = 0)
|
||||
}
|
||||
val intentFilter = IntentFilter(StartService.SERVICE_STOPPED_ACTION)
|
||||
registerReceiver(receiver, intentFilter)
|
||||
StartService.isServiceStarted = false
|
||||
val serviceIntent = Intent(this, StartService::class.java)
|
||||
this.stopService(serviceIntent)
|
||||
}
|
||||
|
||||
private fun logout() {
|
||||
|
@ -28,8 +28,8 @@ class StartService : Service() {
|
||||
|
||||
companion object : FailureHandler {
|
||||
private const val WAKE_LOCK_TAG = "NextPush:StartService:lock"
|
||||
const val SERVICE_STOPPED_ACTION = "org.unifiedpush.distributor.nextpush.services.STOPPED"
|
||||
|
||||
var service: StartService? = null
|
||||
var isServiceStarted = false
|
||||
var wakeLock: PowerManager.WakeLock? = null
|
||||
|
||||
@ -57,6 +57,7 @@ class StartService : Service() {
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
service = this
|
||||
Log.i(TAG, "StartService created")
|
||||
val notification = createForegroundNotification(this)
|
||||
startForeground(NOTIFICATION_ID_FOREGROUND, notification)
|
||||
@ -84,7 +85,7 @@ class StartService : Service() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun stopService() {
|
||||
fun stopService(block: () -> Unit = {}) {
|
||||
Log.d(TAG, "Stopping Service")
|
||||
isServiceStarted = false
|
||||
clearFails()
|
||||
@ -96,9 +97,8 @@ class StartService : Service() {
|
||||
it.release()
|
||||
}
|
||||
}
|
||||
val i = Intent(SERVICE_STOPPED_ACTION)
|
||||
sendBroadcast(i)
|
||||
stopSelf()
|
||||
block()
|
||||
}
|
||||
|
||||
private fun startService() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user