If IsNumber not working

Bryan

Member
Licensed User
Longtime User
I working on a service mod and having a problem with IsNumber.

I'll make this as short as possible to show what I doing. Other things were remove so as to just focus on the problem.

B4X:
Sub Process_Globals
       Dim test_1 As String

Sub Service_Create
       lc.SetVariableAndValue("test_1", "256")

Sub Service_Start (StartingIntent As Intent)
End Sub

Sub chknum
      Dim chkifnum As Int
          If IsNumber(test_1) Then         'this does not work for me.
          Msgbox("OK", "")
          End If
End Sub

This works just fine in the Main Activity but not in the service mod I added.
I tried renaming the variable to test1 in all instances and that did not work either.
Any help is greatly appreciated.

Thnks,
Bryan
 

Bryan

Member
Licensed User
Longtime User
You are correct "IsNumber" does indeed work. What is not working is
lc.SetVariableAndValue("test_1", "256").
256 never gets assigned to test_1 variable.
As previously shown I do declare it as a string.
So instead of using lc.SetVariableAndValue (deleted that line) I assigned 256 to string variable test_1 in the lc_allow sub. Then IsNumber returns true.
Because lc_Allow sub runs, license checking has passed.

It passes also in my Main Activity where it is checked.
I do use lc.SetVariableAndValue("test_1", "256") in my Main Activity and test_1 does get assigned 256 which is tested by IsNumber and returns true.

For Now I will assign a value to test_1 in the lc_allow sub in the service mod.
So I can continue with the rest of the project.


Bryan
 
Upvote 0

Bryan

Member
Licensed User
Longtime User
Maybe I'm not understanding the basic concepts here correctly. I thought the Main activity does not run until the user starts it. So the variables in Process Globals of the Main Activity are not declared until that happens.
My application consists of a Main Activity which logs Date/Time to a file and a service mod added to it for a Widget that also is used to log Date/Time to the same file.
On the boot up of the android OS the Widget's service is loaded. So, Main Activity is running then but not Visible? I did not think Main Activity code runs until it is started by clicking on the App Icon. If the Service Mod can use the Main Activity's Variables why does the Service Mod have a Process Globals? In other words, Main Activity Process Globals are shared with it's attached service mod? :sign0085:
 
Upvote 0

Bryan

Member
Licensed User
Longtime User
Process_Globals variables are always available. You can make the variables private to improve your code encapsulation. However for this variable you will need to make it public.

This would be fine except that if Main Activity_Create which assigns the value to test_1 is not run first then the Service mod main.test_1 never would get a value assigned to it by lc.SetVariableAndValue(test_1, "256").
This would happen if someone were to access a widget created by the service mod before the Main Activity is ever run as an app. There would be no value to pass on to the service mod.

If I have lc.SetVariableAndValue(main.test_1, "256") included in Service_Create it crashes the service with errors. I've also tried putting
Dim lc As LicenseChecker in the Main Activity Process_Globals
and,
included the following in Service_Create. Probably not correct but thought I would try it.


Main.lc.Initialize("lc", GetDeviceId, publicKey, "abcdefg".GetBytes("UTF8"))
Main.lc.SetVariableAndValue(Main.test_1, "256")
Main.lc.CheckAccess

This just crashes the service also.

What do you think can be done here?

I was wondering if there is a way to not allow a service mod from running from within the Main Activity? As if the license check fails in the Main activity then no widget is displayed in the widget listings.

Thanks,
Bryan
 
Last edited:
Upvote 0

Bryan

Member
Licensed User
Longtime User
I'll try to make this as simple as I can by taking out parts of the code that is unrelated to the problem.

Main Activity

B4X:
Sub Process_Globals
     Dim publicKey As String
     publicKey ="abcdefg123...."
     Dim lc As LicenseChecker
     Dim p As Phone
     Dim test_1 As String
End Sub

Sub Globals
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Dim TextRd As TextReader
    Dim line As String
If FirstTime Then
    lc.Initialize("lc", p.GetSettings("android_id"),publicKey, "eeefff".GetBytes("UTF8"))
    lc.SetVariableAndValue("test_1", "256")
    lc.CheckAccess
End if
End Sub

Sub lc_Allow
Log("Allow")
ToastMessageShow("License Valid", True)
End Sub

Sub lc_DontAllow
Log("DontAllow")
ToastMessageShow("Not Licensed", True)
Activity.Finish
End Sub

Sub lc_Error (ErrorCode As String)
Log("error: " & ErrorCode)
ToastMessageShow("Error Closing application.", True)
Activity.Finish
End Sub



Service Mod

B4X:
Sub Process_Globals
End Sub

Sub Service_Create
      rv = ConfigureHomeWidget("My_Widget", "rv", 0, "My_Logger")
      Main.lc.Initialize("lc", Main.p.GetSettings("android_id"), Main.publicKey, "eeefff".GetBytes("UTF8"))
      Main.lc.SetVariableAndValue(Main.test_1, "256")
      Main.lc.CheckAccess
End Sub

Sub Service_Start (StartingIntent As Intent)
      If rv.HandleWidgetEvents(StartingIntent) Then Return
End Sub

Sub lc_Allow
Log("Allow")
ToastMessageShow("License Valid", True)
End Sub

Sub lc_DontAllow
Log("DontAllow")
ToastMessageShow("Not Licensed", True)
End Sub

Sub lc_Error (ErrorCode As String)
Log("error: " & ErrorCode)
ToastMessageShow("Error Closing application.", True)
End Sub

Sub rv_RequestUpdate    
rv.UpdateWidget
End Sub

Sub rv_Disabled    
StopService("")
End Sub

Sub Service_Destroy
End Sub


The errors, which occur when I put the widget on the home screen. It displays fine but generates these errors, and the service quits.


B4X:
Installing file.
PackageAdded: package:com.tc.logger
** Service (service_mod) Create **
unexpected error.
service_mod_service_create (B4A line: 15)
Main.lc.CheckAccess
java.lang.RuntimeException: java.lang.NoSuchFieldException: _
   at com.android.vending.licensing.ServerManagedPolicy.allowAccess(SourceFile:275)
   at com.android.vending.licensing.LicenseChecker.checkAccess(SourceFile:133)
   at anywheresoftware.b4a.objects.LicenseCheckerWrapper.CheckAccess(SourceFile:60)
   at com.tc.logger.service_mod._service_create(service_mod.java:281)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:167)
   at anywheresoftware.b4a.BA.raiseEvent(BA.java:151)
   at com.tc.logger.service_mod.onCreate(service_mod.java:42)
   at android.app.ActivityThread.handleCreateService(ActivityThread.java:1949)
   at android.app.ActivityThread.access$2500(ActivityThread.java:117)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:989)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:130)
   at android.app.ActivityThread.main(ActivityThread.java:3687)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
   at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoSuchFieldException: _
   at java.lang.ClassCache.findFieldByName(ClassCache.java:446)
   at java.lang.Class.getDeclaredField(Class.java:666)
   at com.android.vending.licensing.ServerManagedPolicy.allowAccess(SourceFile:269)
   ... 19 more
java.lang.RuntimeException: Unable to create service com.tc.logger.service_mod: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.NoSuchFieldException: _
   at android.app.ActivityThread.handleCreateService(ActivityThread.java:1959)
   at android.app.ActivityThread.access$2500(ActivityThread.java:117)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:989)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:130)
   at android.app.ActivityThread.main(ActivityThread.java:3687)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
   at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.NoSuchFieldException: _
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:193)
   at anywheresoftware.b4a.BA.raiseEvent(BA.java:151)
   at com.tc.logger.service_mod.onCreate(service_mod.java:42)
   at android.app.ActivityThread.handleCreateService(ActivityThread.java:1949)
   ... 10 more
Caused by: java.lang.RuntimeException: java.lang.NoSuchFieldException: _
   at com.android.vending.licensing.ServerManagedPolicy.allowAccess(SourceFile:275)
   at com.android.vending.licensing.LicenseChecker.checkAccess(SourceFile:133)
   at anywheresoftware.b4a.objects.LicenseCheckerWrapper.CheckAccess(SourceFile:60)
   at com.tc.logger.service_mod._service_create(service_mod.java:281)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:507)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:167)
   ... 13 more
Caused by: java.lang.NoSuchFieldException: _
   at java.lang.ClassCache.findFieldByName(ClassCache.java:446)
   at java.lang.Class.getDeclaredField(Class.java:666)
   at com.android.vending.licensing.ServerManagedPolicy.allowAccess(SourceFile:269)
   ... 19 more


I think I did every thing right as far as using Public Globals declared in the Main Activity.

Thanks,
Bryan
 
Upvote 0

Bryan

Member
Licensed User
Longtime User
I had done it like that. Received same errors. Reason I have Main.lc.SetVariableAndValue("test_1", "256") listed in my code above is that I recreated it just for this posting. It actually was Main.lc.SetVariableAndValue("Main.test_1", "256"). I stripped out all the licensing code so I could continue working on the rest of the code. So as I was posting the above code I just forgot to add the"Main.test_1". Anyway I decided not to try and license the Widget service. What I am doing now is have the Main Activity just do the licensing and be responsible for creating the *.txt file needed for the logging. The Widget will not be able to log date/time unless the Main Activity passes licensing and creates the text file.
So, I figure there is no real need for the licensing in the service mod. I am greatful for your help. It actually helped me understand more about how your compiler works. I have no doubt that I will still have many questions in the future as I continue working with this compiler.

Thanks,
Bryan
 
Upvote 0
Top