Android Question Notification Service with HTTP

Douglas Farias

Expert
Licensed User
Longtime User
hi all.
i need to make a simple check on my website to see if exist a new user mensage and show a notification if exist = true.

i m trying to make this with PostString, this works fine when the app is opned, but when i close the app, i press exit on my main activity the service stop, with Service.StartForeground, my service stop too.

why this? what the best way to make a simple notification with a timer
2 - 2 hours go to my website and check if userid have new mensage, if have show notification.

can someone help me pls? i m many time working on this and not sucess =(

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

Sub Process_Globals
    Dim Notification1 As Notification
    Dim tempo As Timer
    Dim contador As Int
    Dim target, fp As String
    Dim kv As KeyValueStore
    Dim userid As Int
End Sub
Sub Service_Create
    If File.ExternalWritable Then target = File.DirDefaultExternal Else target = File.DirInternal
    File.MakeDir(target, "lala")
     fp = File.Combine(target,"lala")
  
    kvs.Initialize(fp, "data")
  
    tempo.Initialize("Timer", 1000)
    Notification1.Initialize
    Notification1.Icon = "icon"
    Notification1.Light = True
    Notification1.Sound = True
    Notification1.Vibrate = True
    Notification1.SetInfo("lalala", "lalala", Main)
  
    userid = kvs.GetObject("UserID")
    tempo.Enabled = True
    Service.StartForeground(1, Notification1)
End Sub

Sub Service_Start (StartingIntent As Intent)
End Sub

Sub Service_Destroy

End Sub

Sub Timer_Tick
    contador = contador + 1
    If contador = 10 Then
    recebemensagens
    contador = 0
    End If
End Sub


Sub recebemensagens
    Dim recebemensagem As HttpJob
    recebemensagem.Initialize("resultadorecebemensagem", Me)
    recebemensagem.PostString("http://lalal.com/user/", "data=UserID:"&userid)
End Sub


Sub JobDone (job As HttpJob)
    If job.Success Then
    Select job.JobName
    Case "resultadorecebemensagem"
  Notification1.SetInfo("lala", "Nome mensagem", Main)
    Notification1.Notify(1)
    End Select  
    Else
    Notification1.SetInfo("lala", "Nome mensagem", Main)
    Notification1.Notify(1)
    ProgressDialogHide
    ExitApplication
    End If
    job.Release
End Sub

its possible to use keyvaluestore + httppost in a service to check my website every 2 hours? the services allow keyvalustore and http?
 

Straker

Active Member
Licensed User
Longtime User
I don't understand if you need to check the server every 2 hours or every 10 seconds as your timer does.
A service so 'short' will be easily destroyed by the O.S. itself because it needs a lot of resources.

If it's enough to check the server every 10-20 minutes it's better not to use a timer. Instead of the timer you should use StartServiceAt. With this you can declare WHEN the service should start. You should place the StartServiceAt also in your Sub Service_Start, this way the each time the service starts, it reschedules itself for the next run.
 
Upvote 0

Douglas Farias

Expert
Licensed User
Longtime User
i found the problem, very strange
the notification dont show when i use exitapplication to close the app
i need to use activity.finish and now is working correctly.

the 10 seconds is only to test xD

thx man
 
Upvote 0

Straker

Active Member
Licensed User
Longtime User
i found the problem, very strange
the notification dont show when i use exitapplication to close the app
i need to use activity.finish and now is working correctly.

the 10 seconds is only to test xD

thx man

As Erel says:
ExitApplication should kill the process. Note however that this is not the correct way to "close" an app. You should instead call Activity.Finish.
 
Upvote 0
Top