[英]can't accesso to activity object from process global routine
I'm using b4a 11.20 and I need to use a variable in Main on a service module.我正在使用 b4a 11.20,我需要在服务模块的 Main 中使用一个变量。 Main code
主要代码
Sub Process_Globals
Public targetprice As String
End Sub
Sub Globals
Private btnStop As Button
Private btnStart As Button
Private price As EditText
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("layMain")
If FirstTime Then
If File.Exists(File.DirInternal, "1.txt") Then
price.Text = File.ReadString(File.DirInternal, "1.txt")
targetprice= price.text
End If
End If
End Sub
Private Sub btnStart_Click
btnStart.Enabled = False
CallSubDelayed(MyService, "Start")
btnStop.Enabled = True
End Sub
Private Sub btnStop_Click
btnStop.Enabled = False
CallSubDelayed(MyService, "Stop")
btnStart.Enabled = True
End Sub
Private Sub Price_TextChanged (Old As String, New As String)
File.WriteString(File.DirInternal, "1.txt", price.Text)
targetprice= price.text
End Sub
and the service module和服务模块
Sub Process_Globals
Private nid As Int = 1
Private lock As PhoneWakeState
Private timer1 As Timer
End Sub
Sub Service_Create
lock.PartialLock
timer1.Initialize("Timer1", 3000)
End Sub
Sub Service_Start (StartingIntent As Intent)
Service.StopAutomaticForeground
End Sub
Public Sub Start
Service.StartForeground(nid, CreateNotification("HotBit attiva"))
timer1.Enabled = True
End Sub
Public Sub Stop
timer1.Enabled = False
Service.StopForeground(nid)
End Sub
Sub CreateNotification (Body As String) As Notification
Dim notification As Notification
notification.Initialize2(notification.IMPORTANCE_HIGH)
notification.Icon = "icon"
notification.SetInfo("App attiva", Body, Main)
Return notification
End Sub
Sub Service_Destroy
lock.ReleasePartialLock
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")
Log(result)
End If
If result > Main.targetprice Then
Dim n As Notification = CreateNotification($"Prezzo superiore a "$ + result)
n.Notify(nid)
End If
j.Release
End Sub
As you can see, I want to use the variable targetprice from main to service module .如您所见,我想使用变量targetprice从main到service module 。 I declared
Public targetprice as String
on process global on main , and whenever I enter any value on the editText(Price) , or using its textchanged event , I'm using targetprice = Price.Text
我在main上的全局进程中将 Public targetprice 声明
Public targetprice as String
,并且每当我在editText(Price)上输入任何值或使用其textchanged 事件时,我都在使用targetprice = Price.Text
So from Service Module I can call now If result > Main.targetprice Then
, but It doesn't do anything, when i debug it, it will close itself when the timer will fire.因此,我现在可以从服务模块调用
If result > Main.targetprice Then
,但它什么也没做,当我调试它时,它会在计时器触发时自行关闭。 Log:日志:
Logger connesso a: samsung SM-G985F
--------- beginning of main
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
{"error":null,"result":"0.00002873","id":401651055}
0.00002873
myservice$ResumableSub_timer1_tickresume (java line: 285)
java.lang.NumberFormatException: For input string: "Prezzo superiore a"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at lm.hotbit.myservice$ResumableSub_timer1_tick.resume(myservice.java:285)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:267)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:207)
at anywheresoftware.b4a.keywords.Common$11.run(Common.java:1178)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8641)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:567)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1133)
Thanks谢谢
I don't really know b4x ecosystem, but reading logs I think the variable is not a problem.我不太了解 b4x 生态系统,但阅读日志我认为变量不是问题。
java.lang.NumberFormatException: For input string: "Prezzo superiore a"
java.lang.NumberFormatException:对于输入字符串:“Prezzo Superiore a”
probably means you are not building correctly the string.可能意味着您没有正确构建字符串。
concatenation symbol + should be replaced with & , so the following should fix the issue.连接符号+应替换为& ,因此以下内容应解决此问题。
Dim n As Notification = CreateNotification($"Prezzo superiore a "$ & result)
The TextChanged event should be TextChanged 事件应该是
File.WriteString(File.DirInternal, "1.txt", price.Text)
targetprice =price.Text
and since you are dealing with numbers and not strings , Public targetprice As String must be replaced with Public targetprice As Double并且由于您处理的是数字而不是字符串,因此必须将Public targetprice As String替换为Public targetprice As Double
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.