Android Question simple notification action

drgottjr

Expert
Licensed User
Longtime User
B4A. just point me, please:

i have a service that streams network audio. when user moves away from main to check email while still continuing to listen to the radio, a notification indicates that if she taps it, the service will stop the audio play (not the service, just the play). whether or not she changes back to main app later is up to her. but tapping the notification stops the audio.

i get the impression that i can only stop the audio but starting up the main activity again and have it tell the service to stop the audio (use setinfo2 to trigger activity_resume and extract tag). is this correct?

is there a way to call a sub in the service by tapping the notification and not having to switch task back to main? tks

-go
 

drgottjr

Expert
Licensed User
Longtime User
thanks. if i could point to a small service, i would simply point to my service (which happens to be small and, luckily, already running).
according to the documentation - as i read it - the only thing you can point to from a notification tap is an activity...

which leads me to consider 2 possible solutions:
1- a small dummy activity with two jobs: a) deal with the service and b) start the "real" activity.
or
2- erel's famous starter service, which - regrettably - i simply don't get. maybe that's what's needed here, but i can't get my head around it

anyway, thanks for the suggestion
 
Upvote 0

An Schi

Well-Known Member
Licensed User
The notification builder library from barx has buttons in the notification. Maybe you can use them to stop the play directly.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
not sure shame applies here (unless you actually have a simpler solution which you've forgotten :))

so, barx is one courageous dude; i noticed the library while researching my issue. i can't help thinking if that monumental piece of work can talk directly to a service, then there should be a smaller way to do that. i'll check out his example. thanks.
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
There are a couple of ways you can go about this.

1) use 'Actions', these are the buttons you see under notifications. You could have 'Play' and 'Stop' Actions. Or just one Action that updates to 'Play' or 'Stop' accordingly.

2) Use the notification Tag property to store the current state. So, when you audio is playing, store 'playing' in the tag, when itis stopped store 'stopped'. Then when the service resumes, check the tag and act accordingly.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
pretty much to the point. thanks much.

just to clarify:

solution 1: are you saying that 'Actions' talks directly to the service without the need to resume the activity?

solution 2: what do you mean by "when the service resumes" ? from my understanding the service doesn't stop until somebody stops it. in my implementation, only the play stops/starts. the service starts when the activity is created. there is no service resume, right? (obviously, i could implement things differently, eg, have the service start/stop on play start/stop. i chose to have the service running until the user closes the app, but if your library is expecting something different, i could change things.)
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
i keep harping on "talking directly to the service" (for a notification action) because the documentation (examples attached) i've encountered indicates that an activity is the recipient of a notification action, not a service. erel's core notification code strikes me as the simplest implementation of android's notification builder, and notification builder's action method involves an activity. all the added buttons don't seem to change that basic behavior. that is the case, no?
 

Attachments

  • notification.txt
    1.4 KB · Views: 269
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
@drgottjr have you installed the demo app?

If you run the demo app and then look at the second to bottom section. you will see:

  • Style
  • Stacked
  • Add Actions
select add action and ativate the notification.

The 3rd Action is an example of an action that calls directly to a service ;)
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
working on it. sdk manager update dragging along. will revert... tjs
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
sorry, slight confusion; not clear whether demo required all the developer updates first. anyway, i did plug in the demo and ran it and followed your instructions. it apparently did what it was supposed to do, but i have no idea what that was. i'm sure it makes sense to someone who uses notifications a lot. for my purposes, having the main activity pop up when the user taps the notification is not the worst thing that could happen and is actually looking better and better.

having the service do something directly would have been nice. i see your addaction2() call, which is meant to address a service - as opposed to addaction(), which addresses an activity - so obviously you foresaw the need for what it was that i wanted to do. thanks for your interest and input.
 
Upvote 0
Top