Android Question Service.StartForeground with no notification icon

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I am using the following code to download a file from a web server.

I noticed that there is a notification icon that displays while it downloads the file. I rather that this notification icon doesn't display.

I have tried changing:

B4X:
Service.StartForeground(1,n)

to

B4X:
Service.StartForeground(1,Null)

but that didn't work.

Is there a way in not displaying the notification icon while the file is downloading ?

B4X:
#Region  Service Attributes
    #StartAtBoot: False
#End Region

Sub Process_Globals
    Private jobs As Map
    Private timer1 As Timer
    Type DownloadData (url As String, Target As Object, EventName As String)
    Type JobTag (Data As DownloadData,  _
        CountingStream As CountingOutputStream, Total As Long)
    Private pw As PhoneWakeState
End Sub
Sub Service_Create
    jobs.Initialize
    timer1.Initialize("timer1", 1000)
End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Sub Service_Destroy

End Sub

Private Sub StartTimer (Target As Object)
    Dim n As Notification
    n.Initialize
    n.Icon = "icon"
    n.Vibrate = False
    n.Sound = False
    n.Light = False
    n.SetInfo("Downloading file...", "", Target)
   
    Service.StartForeground(1,n)
    timer1.Enabled = True
    pw.PartialLock
End Sub

Private Sub EndTimer
    Service.StopForeground(1)
    timer1.Enabled = False
    pw.ReleasePartialLock
End Sub

Public Sub StartDownload(data As DownloadData)
    If jobs.ContainsKey(data.url) Then
        Log("Ignoring duplicate request.")
        Return
    End If
    Dim J As HttpJob
    J.Initialize(data.url, Me)
    Dim tag As JobTag
    tag.Initialize
    tag.data = data
    J.tag = tag
    jobs.Put(data.url, J)
    J.Download(data.url)
    If timer1.Enabled = False Then StartTimer(data.Target)
End Sub

Public Sub CancelDownload(url As String)
    If jobs.ContainsKey(url) = False Then
        Log("Ignoring cancel request.")
        Return
    End If
    Dim job As HttpJob = jobs.Get(url)
    Dim jt As JobTag = job.Tag
    If jt.CountingStream.IsInitialized Then
        jt.CountingStream.Close
    Else
        jt.Data.url = ""
    End If
End Sub

Sub timer1_tick
    For Each job As HttpJob In jobs.Values
        Dim jt As JobTag = job.Tag
        If jt.CountingStream.IsInitialized Then
            CallSub3(jt.Data.Target, jt.Data.EventName & "_Progress", _
                jt.CountingStream.Count, jt.Total)
        End If
    Next
End Sub

Sub JobDone(job As HttpJob)
    jobs.Remove(job.JobName)
    Dim jt As JobTag = job.Tag
    If jobs.Size = 0 Then EndTimer
    If job.Success Then
        CallSubDelayed3(jt.Data.Target, jt.Data.EventName & "_Progress", _
                jt.CountingStream.Count, jt.Total)
        CallSubDelayed2(jt.Data.Target, jt.Data.EventName & "_Complete", _
                job)
    Else
        Log(job.ErrorMessage)
        CallSubDelayed2(jt.Data.Target, jt.Data.EventName & "_Complete", _
                job)
    End If
End Sub
 

aaronk

Well-Known Member
Licensed User
Longtime User
Worked it out. (I was over looking it)

In case someone else has the same issue..

removed:
B4X:
Dim n As Notification
    n.Initialize
    n.Icon = "icon"
    n.Vibrate = False
    n.Sound = False
    n.Light = False
    n.SetInfo("Downloading file...", "", Target)
  
    Service.StartForeground(1,n)
 
Upvote 0

asales

Expert
Licensed User
Longtime User
Worked it out. (I was over looking it)

In case someone else has the same issue..

removed:
B4X:
Dim n As Notification
    n.Initialize
    n.Icon = "icon"
    n.Vibrate = False
    n.Sound = False
    n.Light = False
    n.SetInfo("Downloading file...", "", Target)
 
    Service.StartForeground(1,n)

What you removed? The notification? How you see the notification now?

I have an issue with an alarm notification service that's not show the icon in Android 5+.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I have an issue
Like any other Issue: Always create a new thread for your Questions instead of posting to old threads.
 
Upvote 0

pesquera

Active Member
Licensed User
Longtime User
Hi Aaronk,

Could you please share your code? as you have been solved it

I got an error with Null on the second parameter for Service.StartForeground
This one: java.lang.IllegalArgumentException: null notification

Thanks!
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
Could you please share your code?
I did. In post 1 and post 2.

I got an error with Null on the second parameter for Service.StartForeground
This one: java.lang.IllegalArgumentException: null notification
I recommend starting a new thread in the questions forum, and recommend to show the code you are using so we can work out what has gone wrong.

FYI.. I followed a tutorial which showed me how to download a file from a web service. While the file was downloading it showed a icon in the notification bar but I didn't want the icon to show while it was downloading. Look at post 1 to see the original code I was using, and then look at post 2 what code I removed to stop the icon from displaying while it was downloading.
 
Upvote 0
Top