Android Question Keeping the foreground service alive

Silkowner

New Member
I have a program and it needs to always be running in the foreground. Do you think the following code is sufficient?



Starter:
#Region  Service Attributes
    #StartAtBoot: True
    #ExcludeFromLibrary: True
    #StartCommandReturnValue: android.app.Service.START_STICKY
#End Region

Sub Process_Globals
    Private n As Notification
    Dim awak As PhoneWakeState
    Dim pd As PersianDeviceInfo
    Private channelId As String = "ServiceChannel"
    Dim phone As Phone
    Public IsRunning As Boolean = False
    Private snotif As PhoneEvents
End Sub

Sub Service_Create
    snotif.Initialize("ScreenEvents")
    
    CreateNotificationChannel
    Dim n As Notification
    n.Initialize2(n.IMPORTANCE_LOW)
    n.Icon = "icon3"
    n.Sound = False
    n.Vibrate = False
    n.Light = False
    n.Insistent = False
    n.AutoCancel = False
    n.OnGoingEvent = True
    n.SetInfo("بروزرسانی در دسترس است", "", "")
    
    Service.StartForeground(1, n)
    
    awak.PartialLock
    awak.KeepAlive(False)
    IsRunning = True
    CallSubDelayed(FirebaseMessaging, "SubscribeToTopics")

End Sub

Sub CreateNotificationChannel
    Dim importance As Int = 2
    pd.initialize("")
    If phone.SdkVersion >= 26 Then
        Try
            Dim channel As JavaObject
            channel.InitializeNewInstance("android.app.NotificationChannel", Array(channelId, "Service Channel", importance))
            channel.RunMethod("enableLights", Array(False))
            channel.RunMethod("enableVibration", Array(False))
            
            Dim manager As JavaObject
            manager.InitializeContext
            manager = manager.RunMethod("getSystemService", Array("notification"))
            manager.RunMethod("createNotificationChannel", Array(channel))
        Catch
            Log("Error creating notification channel: " & LastException)
        End Try
    End If
End Sub

Sub Service_Start (StartingIntent As Intent) As Int
    Try   
        CreateNotificationChannel
        Dim n As Notification
        n.Initialize2(n.IMPORTANCE_LOW)
        n.Icon = "icon3"
        n.Sound = False
        n.Vibrate = False
        n.Light = False
        n.Insistent = False
        n.AutoCancel = False
        n.OnGoingEvent = True
        n.SetInfo("بروزرسانی در دسترس است", "", "")
        Service.StartForeground(1, n)
        IsRunning = True
        CallSubDelayed(FirebaseMessaging, "SubscribeToTopics")
    Catch
        Log("Error in Service_Start: " & LastException)
    End Try

    Return 1
End Sub



Sub Service_TaskRemoved
    IsRunning = False
    StartServiceAt(RestartService, DateTime.Now + 1000, False)
End Sub



Sub Service_Destroy
    Try
        awak.ReleasePartialLock
    Catch
        Log("kir to akuma(shirin)")
    End Try
    
    IsRunning = False
    
    StartServiceAt(RestartService, DateTime.Now + 1000, False)
End Sub


Sub ScreenEvents_ScreenOn (Intent As Intent)
    Try
        If IsRunning = False Then
            StartServiceAt(RestartService, DateTime.Now + 1000, False)
        End If
    Catch
        Log("fack you sector")
    End Try
End Sub
 

Similar Threads

Replies
4
Views
2K
Replies
48
Views
39K
Replies
15
Views
51K
Replies
185
Views
228K
Cookies are required to use this site. You must accept them to continue using the site. Learn more…