Android Question service module

Isac

Active Member
Licensed User
Longtime User
Hello,

I have a problem with the service module: If I turn off and then I turn on the phone the alarm does not work.

I tried to use: # StartAtBoot: True

But as soon as I turn on the phone immediately starts the service module, but this is not good.

thanks
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
You run into the same problem like the one in this thread. See the comments there and you´ll have a solution for your problem ;)
 
Upvote 0

Isac

Active Member
Licensed User
Longtime User
Erel how can I change the service module?
I have no idea, now when I turn on the phone sevice module starts by itself.

thanks

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

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
   
    Dim n As Notification
End Sub
Sub Service_Create

End Sub

Sub Service_Start (StartingIntent As Intent)

n.Initialize
n.Icon = "icon"
n.SetInfo("warning ", "You have reached the date","")
n.Notify(1)
End If
End Sub

Sub Service_Destroy
    StopService("")    'Stop this service
    CancelScheduledService("")  'Cancel this service
End Sub
 
Upvote 0

Isac

Active Member
Licensed User
Longtime User
Erel I created the new service module, now how do I share the given variable of the module Main?


B4X:
 #Region  Service Attributes
    #StartAtBoot: true
#End Region
 
 
 
 
Sub Process_Globals
 
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
 
 
End Sub
Sub Service_Create
 
End Sub
 
Sub Service_Start (StartingIntent As Intent)
 
StartServiceAt(start,date,False)
 
End Sub
 
Sub Service_Destroy
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
Hi,
To retrieve Process_Globals you have to reference it by the module's name and the variable name.
For example : Main.Variable_Name

Of course, Variable_Name should not be Private

Tip: Sometimes it is easier to store the content of the variable in a file and retrieve it from the running service. Just an idea in case of.
 
Upvote 0

Isac

Active Member
Licensed User
Longtime User
thanks

I tried to insert Main.date, but the variable does not see it.



Parsing code. Error
Error parsing program.
Error description: Undeclared variable 'date' is used before it was assigned any value.
Occurred on line: 19
StartServiceAt(start,date,False)

B4X:
 #Region  Service Attributes
    #StartAtBoot: true
#End Region
 
Sub Process_Globals
 
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
   
    Main.date
   
End Sub
Sub Service_Create
 
End Sub
 
Sub Service_Start (StartingIntent As Intent)
 
StartServiceAt(start,date,False)
 
End Sub
 
Sub Service_Destroy
 
End Sub
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
Excuse, this is not the correct usage.

In the Main activitiy

B4X:
Sub Process_Globals
  Dim Date1 as Long
End Sub

And in the Service module you make the reference to the Main.date1


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

Sub Process_Globals
'These global variables will be declared once when the application starts.'These variables can be accessed from all modules.

End Sub

Sub Service_Create
End Sub

Sub Service_Start (StartingIntent AsIntent)
  StartServiceAt(start,Main.date1,False)
  StopService("")
End Sub

Sub Service_Destroy
End Sub
 
Upvote 0

Isac

Active Member
Licensed User
Longtime User
Thanks lemonisdead,

That's fine but when I restart the phone the service module will start automatically without waiting for launch date.



this is the first service module called (boot)

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

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub
Sub Service_Create
End Sub


Sub Service_Start (StartingIntent As Intent)

StartServiceAt(start,Main.date,False)
StopService("")

End Sub

Sub Service_Destroy

End Sub

This is the second service module called (start)

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

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
  
    Dim n As Notification
End Sub
Sub Service_Create

End Sub

Sub Service_Start (StartingIntent As Intent)

n.Initialize
n.Icon = "icon"
n.SetInfo("warning ", "You have reached the date","")
n.Notify(1)

End Sub

Sub Service_Destroy
    StopService("")    'Stop this service
    CancelScheduledService("")  'Cancel this service
End Sub

this is a piece of code Main

B4X:
date=DateTime.Add(DATA,0,0,Giorni)

StartServiceAt(boot,date,False)


I do not understand why the service module boot starts without waiting for the date.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
On device reboot the date you set is lost... Save the date when setting the servicestartat to somewhere... Key value store, db, statemanger, whatever.. On service start at boot you check whether the desired date is reached or not. If not then set the new startat and quit. If the date is reached or in ast then do the work.

Search the forum. There was a few threads handling this issue...
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
Hi Isac,
You can use several methods. You could take a look at the File keyword. For example, you can save a list or a map with the useful information.
 
Upvote 0

Isac

Active Member
Licensed User
Longtime User
Hi Lemonisdead,

I could use the TextWriter to save the date of a txt file?
and then recall it with TextReader?

thanks
 
Upvote 0

Isac

Active Member
Licensed User
Longtime User
I am using the class Keyvaluestore:
But I need your help because it does not work.

This main module
B4X:
dim memoria as Keyvaluestore
date=DateTime.Add(DATA,0,0,Giorni)
memoria.Initialize(File.DirDefaultExternal,"datastore") 
memoria.PutSimple("date",date) 
StartServiceAt(boot,date,False)


This module boot
B4X:
Sub Process_Globals
 
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
     
dim memoria as Keyvaluestore
 
 
End Sub
Sub Service_Create
 
End Sub
 
Sub Service_Start (StartingIntent As Intent)
memoria.Initialize(File.DirDefaultExternal,"datastore") 
DateTime.date(memoria.GetSimple("date"))
StartServiceAt(starts,"date",False)
StopService("")

Starsts module is OK
 
Last edited:
Upvote 0

Isac

Active Member
Licensed User
Longtime User
In compiling no errors but when I turn off and turn on the phone comes out a message saying that the application will be blocked.
 
Upvote 0
Top