how to remove the Notification from status bar when service stop?

sally3599

Member
Licensed User
Longtime User
main activity:

B4X:
Sub Process_Globals

End Sub

Sub Globals
      Dim StartButton As Button
   Dim StopButton As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)

    Activity.LoadLayout("Main")
   
    If IsPaused(mp3service)=True Then
       StartButton.Enabled = True
       StopButton.Enabled = False
    Else
       StartButton.Enabled = False
       StopButton.Enabled = True
    End If

End Sub


Sub Activity_Resume

End Sub

Sub Activity_Pause(UserClosed As Boolean)

    StopService(mp3service)
            
End Sub


Sub StartButton_Click

  StartButton.Enabled = False
  StopButton.Enabled = True
  StartService(mp3service)
   
End Sub

Sub StopButton_Click

  StartButton.Enabled = True
  StopButton.Enabled = False
  StopService(mp3service)
   
End Sub

service named mp3service:

B4X:
Sub Process_Globals
   Dim notification1 As Notification
End Sub
Sub Service_Create

End Sub

Sub Service_Start (StartingIntent As Intent)
    Dim n As Notification
    n.Initialize
    n.OnGoingEvent = True
    n.Sound = False
    n.Vibrate = False
    n.Icon = "icon"
    n.SetInfo("Volume", "", Main) 
    n.Notify(1)
End Sub

Sub Service_Destroy

End Sub

how to remove the Notification from status bar when StopService(mp3service) ?
Any help will be appreciate it.
 

sally3599

Member
Licensed User
Longtime User
Another question

Thanks cmweb.

B4X:
Sub Service_Destroy
   n.Cancel(1)
End Sub

But when I pulled down the status bar to expanded messages, then click the notification generated from n.Notify(1), the icon of n.Notify(1) on the top disappear !! but the service (mp3service) is still running !

How can the icon of notification on the top keep showing if service (mp3service) is still running ?

Any help will be appreciate it.
 
Upvote 0

sally3599

Member
Licensed User
Longtime User
it doesn't work

Thanks cmweb.

n.autocancel = false ==> it doesn't work
and also go to somewhere else, icon on the top disappear too.
 
Upvote 0

cmweb

Active Member
Licensed User
Longtime User
What is your complete code?

And what do you mean with "go to somewhere else"?

Best regards,

Carsten
 
Upvote 0

sally3599

Member
Licensed User
Longtime User
here is the zip

Thanks cmweb.

The attach file is complete code.
"go to somewhere else" means leave the app.
 

Attachments

  • Notification_service.zip
    7.4 KB · Views: 228
Upvote 0

sally3599

Member
Licensed User
Longtime User
actually a service load mp3 also crash!

Here is the code:
B4X:
'Service module
Sub Process_Globals
   Dim n As Notification
   Dim mp As MediaPlayerStream
End Sub
Sub Service_Create
    mp.Load("http://website/my.mp3")
End Sub

Sub Service_Start (StartingIntent As Intent)
   'mp.Play
    n.Initialize
    n.OnGoingEvent = True
    n.Sound = False
    n.Vibrate = False
    n.Icon = "icon"
    n.SetInfo("Volume", "", Main) 
   n.autocancel = False
    n.Notify(1)
   
End Sub

Sub Service_Destroy
    'mp.Stop
   n.Cancel(1)
End Sub

It will crash when click the button of "Start Service".
I found the problem is : mp.Load("http://website/my.mp3")
Any help will be appreciate it.
 

Attachments

  • Notification_service_load_mp3.zip
    7.5 KB · Views: 185
Upvote 0

cmweb

Active Member
Licensed User
Longtime User
Thanks cmweb.

The attach file is complete code.
"go to somewhere else" means leave the app.

The problem is, that the service has stopped since it is running only one time.

The service sets the notification, and cancels the notification again...

This code doesn't make sense.

Put the notification stuff into your Main activity and it works.

Best regards,

Carsten
 
Upvote 0

cmweb

Active Member
Licensed User
Longtime User
Here is the code:
B4X:
'Service module
Sub Process_Globals
   Dim n As Notification
   Dim mp As MediaPlayerStream
End Sub
Sub Service_Create
    mp.Load("http://website/my.mp3")
End Sub

Sub Service_Start (StartingIntent As Intent)
   'mp.Play
    n.Initialize
    n.OnGoingEvent = True
    n.Sound = False
    n.Vibrate = False
    n.Icon = "icon"
    n.SetInfo("Volume", "", Main) 
   n.autocancel = False
    n.Notify(1)
   
End Sub

Sub Service_Destroy
    'mp.Stop
   n.Cancel(1)
End Sub

It will crash when click the button of "Start Service".
I found the problem is : mp.Load("http://website/my.mp3")
Any help will be appreciate it.
You're now talking about a totally different issue...

Best regards,

Carsten
 
Upvote 0

sally3599

Member
Licensed User
Longtime User
what's the Process_Globals different between Main Activity and Service

Hello cmweb,

It's working only for both Main Activity and Service need to "Dim n As Notification" first.

Service:
B4X:
Sub Process_Globals
   Dim n As Notification
End Sub
Sub Service_Create

End Sub

Sub Service_Start (StartingIntent As Intent)
    n.Initialize
    n.OnGoingEvent = True
    n.Sound = False
    n.Vibrate = False
    n.Icon = "icon"
    n.SetInfo("Volume", "the content", Main) 
   n.autocancel = False
End Sub

Sub Service_Destroy

End Sub

And Main Activity:
B4X:
Sub Process_Globals
    Dim n As Notification
End Sub

Sub StartButton_Click

  StartButton.Enabled = False
  StopButton.Enabled = True
  StartService(mp3service)
  n.Notify(1)
End Sub

Sub StopButton_Click

  StartButton.Enabled = True
  StopButton.Enabled = False

  StopService(mp3service)
  n.Cancel(1)
End Sub

n.Initialize in Service and Hide/Show Notification in Main Activity, what's the Process_Globals different between Main Activity and Service ?

Any help will be appreciate it.
 
Upvote 0

cmweb

Active Member
Licensed User
Longtime User
Unfortunately, I don't understand nothing.

What do you want exactly?

As I said - put all this:

B4X:
n.Initialize
    n.OnGoingEvent = True
    n.Sound = False
    n.Vibrate = False
    n.Icon = "icon"
    n.SetInfo("Volume", "the content", Main) 
    n.autocancel = False

into the StartButton_click of your main activity and it works: Notification will be fired when you push the Start button. And Notification will not disappear when tapping on it. And notification will be cancelled by pushing the StopButton.

The basic problem of your code was that the service runs once and is destroyed then. So of course, the notification will gone...

I don't understand your last question. What do you mean with "Process_Globals different between Main Activity and Service"?

Best regards,

Carsten
 
Upvote 0
Top