Sub Globals
'These global variables will be redeclared each time the activity is created.
Private Button1 As Button
Private Label1 As Label
Private timer1 As Timer
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("layout")
timer1.Initialize("Timer1", 100)
timer1.Enabled=True
End Sub
Sub Button1_Click
timer1.Enabled=False
End Sub
Sub timer1_tick
Dim j As HttpJob
j.Initialize("", Me)
j.Download("https://api.hotbit.io/api/v1/market.last?market=KIBA/USDT")
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
Log(j.GetString)
Dim parser As JSONParser
parser.Initialize(j.GetString)
Dim root As Map = parser.NextObject
Dim result As String = root.Get("result")
Label1.Text= result
End If
j.Release
End Sub
It was last updated a few months ago. You can see it in the bottom right corner. Using a foreground service is the only way to keep your app running in the background, and it will also won't work on all devices.It is 3 ys old and I don't know if that is all I need.
SetServiceAttribute(Tracker, android:foregroundServiceType, "location")
Using a foreground service is the only way to keep your app running in the background
No. You need to learn about services.Please tell me if I'm wrong but is that sentence an oxymoron?
But the service that you are trying to run in the "background" needs to show a notification in order to run. Therefore it's a foreground service (the user is aware of it's existence due to the notification icon). The naming is from Google itself, so blame them for such wording. True background services, without notification icon, are getting more difficult with each Android release and have their own restrictions/constraints.because I obviously can't keep the app in foreground 24/7
But the service that you are trying to run in the "background" needs to show a notification in order to run. Therefore it's a foreground service (the user is aware of it's existence due to the notification icon). The naming is from Google itself, so blame them for such wording. True background services, without notification icon, are getting more difficult with each Android release and have their own restrictions/constraints.
SetServiceAttribute(Tracker, android:foregroundServiceType, "location")
CallSubDelayed(FirebaseMessaging, "SubscribeToTopics")
#Region Project Attributes
#ApplicationLabel: B4A Example
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
#BridgeLogger: True
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Private xui As XUI
Private timer1 As Timer
Private const API_KEY As String = "api key"
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
Private Button1 As Button
Private Label1 As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("layout")
timer1.Initialize("Timer1", 3000)
timer1.Enabled=True
End Sub
Sub Button1_Click
timer1.Enabled=False
End Sub
Sub timer1_tick
Dim j As HttpJob
j.Initialize("", Me)
j.Download("https://api.hotbit.io/api/v1/market.last?market=KIBA/USDT")
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
Log(j.GetString)
Dim parser As JSONParser
parser.Initialize(j.GetString)
Dim root As Map = parser.NextObject
Dim result As String = root.Get("result")
Label1.Text= result
End If
If result > "0.00002874" Then
CallSubDelayed(FirebaseMessaging, "SubscribeToTopics")
SendMessage("general", "Price increased above 0.00002874", "Sell")
'StartMessageLoop
End If
j.Release
End Sub
Private Sub SendMessage(Topic As String, Title As String, Body As String)
Dim Job As HttpJob
Job.Initialize("fcm", Me)
Dim m As Map = CreateMap("to": $"/topics/${Topic}"$)
Dim data As Map = CreateMap("title": Title, "body": Body)
If Topic.StartsWith("ios_") Then
Dim iosalert As Map = CreateMap("title": Title, "body": Body, "sound": "default")
m.Put("notification", iosalert)
m.Put("priority", 10)
End If
m.Put("data", data)
Dim jg As JSONGenerator
jg.Initialize(m)
Job.PostString("https://fcm.googleapis.com/fcm/send", jg.ToString)
Job.GetRequest.SetContentType("application/json;charset=UTF-8")
Job.GetRequest.SetHeader("Authorization", "key=" & API_KEY)
End Sub
Sub JobDone(job As HttpJob)
Log(job)
If job.Success Then
Log(job.GetString)
End If
job.Release
ExitApplication '!
End Sub
You can use a VPS which is cheap or free. If this is what you want to avoid then forget it.A server using b4j means I should leave the computer on, which is what I want to avoid
Then you're back with creating a foreground service that may still be killed. There are partial solutions to this issue (getting killed) such as enabling partial lock, turning off battery optimization and every time your app goes in the background setting a timer (min 15 minutes) to start the service again (just in case your app is killed). Some methods would get you flagged on the Play Store, but I don't think your going for an app for publishing. Also note, some phone manufactures are really set on killing applications in the name of battery savings, so you may still end up with an application that dies. With the above three methods I do seem to have decent luck on a Samsung S7 and S9. As to the newer S20, who knows (only testing will tell).I would like to avoid creating a new server since I want the phone itself to be the server and the client together. A server using b4j means I should leave the computer on, which is what I want to avoid.
This setting is only required if you are using a foreground service for location purposes. A standard foreground service does not need this additional Manifest settingSetServiceAttribute(Tracker, android:foregroundServiceType, "location")
Hi olive, thanks for your reply.Then you're back with creating a foreground service that may still be killed. There are partial solutions to this issue (getting killed) such as enabling partial lock, turning off battery optimization and every time your app goes in the background setting a timer (min 15 minutes) to start the service again (just in case your app is killed). Some methods would get you flagged on the Play Store, but I don't think your going for an app for publishing. Also note, some phone manufactures are really set on killing applications in the name of battery savings, so you may still end up with an application that dies. With the above three methods I do seem to have decent luck on a Samsung S7 and S9. As to the newer S20, who knows (only testing will tell).
This setting is only required if you are using a foreground service for location purposes. A standard foreground service does not need this additional Manifest setting
#Region Project Attributes
#ApplicationLabel: B4A Example
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
#BridgeLogger:True
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Private xui As XUI
Private timer1 As Timer
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
Private Button1 As Button
Private Label1 As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("layout")
timer1.Initialize("Timer1", 3000)
timer1.Enabled=True
End Sub
Sub Button1_Click
timer1.Enabled=False
End Sub
Sub timer1_tick
Dim j As HttpJob
j.Initialize("", Me)
j.Download("https://api.hotbit.io/api/v1/market.last?market=KIBA/USDT")
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
Log(j.GetString)
Dim parser As JSONParser
parser.Initialize(j.GetString)
Dim root As Map = parser.NextObject
Dim result As String = root.Get("result")
Label1.Text= result
End If
If result > "0.00002874" Then
'send notification
End If
j.Release
End Sub
Sub Activity_Resume
Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION)
Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
If Result Then
StartService("" )
Else
ToastMessageShow("No permission...", True)
End If
End Sub
#Region Service Attributes
#StartAtBoot: False
#ExcludeFromLibrary: True
#End Region
Sub Process_Globals
Public rp As RuntimePermissions
End Sub
Sub Service_Create
End Sub
Sub Service_Start (StartingIntent As Intent)
End Sub
Sub Service_TaskRemoved
'This event will be raised when the user removes the app from the recent apps list.
End Sub
'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
Return True
End Sub
Sub Service_Destroy
End Sub
#Region Service Attributes
#StartAtBoot: True
#End Region
Sub Process_Globals
Private nid As Int = 1
Private LastUpdateTime As Long
Private lock As PhoneWakeState
End Sub
Sub Service_Create
Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER 'we are handling it ourselves
lock.PartialLock
End Sub
Sub Service_Start (StartingIntent As Intent)
Service.StartForeground(nid, CreateNotification("..."))
StartServiceAt(Me, DateTime.Now + 30 * DateTime.TicksPerMinute, True)
PrezzoCambiato
End Sub
Sub PrezzoCambiato
If DateTime.Now > LastUpdateTime + 10 * DateTime.TicksPerSecond Then
Dim n As Notification = CreateNotification($"Price is higher than n"$)
n.Notify(nid)
LastUpdateTime = DateTime.Now
End If
End Sub
Sub CreateNotification (Body As String) As Notification
Dim notification As Notification
notification.Initialize2(notification.IMPORTANCE_LOW)
notification.Icon = "icon"
notification.SetInfo("Price is higher than n", Body, Main)
Return notification
End Sub
Sub Service_Destroy
lock.ReleasePartialLock
End Sub
Logger connesso a: samsung SM-G985F
--------- beginning of main
Copying updated assets files (1)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (activity_permissionresult)
running waiting messages (1)
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (activity_permissionresult)
running waiting messages (1)
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (activity_permissionresult)
running waiting messages (1)
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (activity_permissionresult)
running waiting messages (1)
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (activity_permissionresult)
running waiting messages (1)
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (activity_permissionresult)
running waiting messages (1)
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (activity_permissionresult)
running waiting messages (1)
** Activity (main) Resume **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (activity_permissionresult)
running waiting messages (1)
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (activity_permissionresult)
running waiting messages (1)
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (activity_permissionresult)
running waiting messages (1)
** Activity (main) Resume **
{"error":null,"result":"0.00002874","id":408249259}
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (activity_permissionresult)
running waiting messages (1)
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (activity_permissionresult)
running waiting messages (1)
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (activity_permissionresult)
running waiting messages (1)
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (activity_permissionresult)
running waiting messages (1)
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (activity_permissionresult)
running waiting messages (1)
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (activity_permissionresult)
running waiting messages (1)
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (activity_permissionresult)
running waiting messages (1)
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (activity_permissionresult)
running waiting messages (1)
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (CallSubDelayed - JobDone)
sending message to waiting queue (activity_permissionresult)
running waiting messages (2)
{"error":null,"result":"0.00002875","id":363420695}
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (activity_permissionresult)
running waiting messages (1)
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (activity_permissionresult)
running waiting messages (1)
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (activity_permissionresult)
running waiting messages (1)
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (activity_permissionresult)
running waiting messages (1)
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="29"/>
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.LightTheme)
'End of default text.
SetServiceAttribute(Parsingandinvio, android:foregroundServiceType, "location")
Sub FetchPrice
Log("Fetching price...")
Dim j As HttpJob
j.Initialize("", Me)
j.Download("https://api.hotbit.io/api/v1/market.last?market=KIBA/USDT")
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
Log(j.GetString)
Dim parser As JSONParser
parser.Initialize(j.GetString)
Dim root As Map = parser.NextObject
Dim result As String = root.Get("result")
'Label1.Text = result
CallSubDelayed2(Main, "ShowPriceLabel", result)
If result.As(Double) > 0.000026 Then
Log($"Alert! ${result}"$)
Starter.RunService = False
CallSubDelayed2(Main, "SetButtonText", "Start Service")
'timer1.Enabled = False
' invia notifica
Service.StartForeground(nid, CreateNotification(result))
Return
Else
Log("pass...")
End If
LastUpdateTime = DateTime.Now
StartServiceAt(Me, LastUpdateTime + 5 * DateTime.TicksPerSecond, True) ' After 5 seconds
End If
j.Release
End Sub
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?