Android Question Notification in a service - have I done it correctly?

pjetson

Member
Licensed User
Longtime User
I have attached a minimal program that seemed to work initially (but see below), but I'm not sure I've done it the "right" way, particularly now I've found a problem.

This minimal program has two buttons, IN and OUT. Pressing the IN button disables that button and enables the OUT button, and vice-versa. You can only be IN or OUT, and the program saves the in/out state so that it can be restored when the program runs again. That's all the main section does.

I want the user to be able to see whether they are IN or OUT even if the program is not in the foreground. So, I've added a sticky service and put notification code in there. Now, each time the user presses a button, the status is shown in the notification icon. And if the user pulls down the notifications, they can tap to get back to the main program.

The problem is that it only seems to work while my phone is plugged in (to the PC that I'm developing on).

If I unplug the phone, then pull down the notification area, then tap on the notification, the main screen opens but the IN and OUT buttons are not displayed. I have turned the phone off then on again, and run the app again (from its icon), but the IN and OUT buttons are still not displayed, whether the phone is connected to the PC or not.

The only way to get the app back to normal seems to be to plug the phone into the PC and download the app again via B4A, and then the buttons appear again.

Obviously, I've done something wrong. Does anyone have any ideas what it might be?

I have two other questions about the program, but I suspect I'd be asked to start a new thread if I put them here, so I'll leave them for later. :)

In case it matters, my phone is a Google Nexus 4 running Android 5.1.1 and B4A is V7.0.1

Thanks,
Peter
 

Attachments

  • minimal2.zip
    12.5 KB · Views: 163

pjetson

Member
Licensed User
Longtime User
Add some log messages and add #ReleaseLogger: True

Hi, Erel. Where do I put #ReleaseLogger: True, and what will it do?

If I put it in #Region Project Attributes or #Region Activity Attributes or #Region Service Attributes I just get an error message that says "Attribute not supported". I can't seem to find any mention of #ReleaseLogger in the forums.

Added later: it appears that #ReleaseLogger may be a B4i only facility.

Peter
 
Last edited:
Upvote 0

pjetson

Member
Licensed User
Longtime User
Yes. A foreground service (Service.StartForeground).

Thanks, Erel. It appears from my reading of the forums that when I use Service.StartForeground(1, n) that I should no longer use n.Notify(1) - is that correct?

You also told me:
Don't use the same Notification object multiple times. Initialize it again each time you want to update it

So, does this mean that when I want to change the Notification object, that I need to first stop the original foreground service, then initialise and set up the Notification object, then start the foreground service again?

Perhaps something like this?
B4X:
Service.StopForeground(1)
n.Initialize
' set the notification parameters here
Service.StartForeground(1, n)

Also, should I have an n.Cancel(1) before the n.Initialize ?

Thanks,
Peter
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Hi, Erel. Where do I put #ReleaseLogger: True, and what will it do?
Please start a new thread for this question (it is a B4A attribute).

that I should no longer use n.Notify(1) - is that correct?
Correct.

that I need to first stop the original foreground service, then initialise and set up the Notification object, then start the foreground service again?
I don't think that you need to stop the service. Try to call StartForeground again with the new notification object.
 
Upvote 0

pjetson

Member
Licensed User
Longtime User
One thing about forums is that you get to see the problems, and the helpful hints and answers, but you often you don't get to see the original code with the fixes in place. So, with that in mind, I've attached my modified minimal program, with most of the suggestions above implemented (as I understand them). So far, it seems to be working, but I thought that originally as well :)

It seems that the Service.StopForeground is not needed, as Erel suggested, and removing it had the welcome side effect of stopping the notification icon from flashing briefly when the buttons are pressed.

I'm still keen to hear suggestions of anything that I might still have not done the right way...

Peter
 

Attachments

  • minimal3.zip
    12.5 KB · Views: 159
Upvote 0
Top