Android Question Starting and Cancelling Services now failing in B4A 8.00

GuyBooth

Active Member
Licensed User
Longtime User
Until B4A version 8.0 this code was working, now it is failing. I am calling services with intent, defining them in a code module called MTO like this:

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
      ' Dimension all the intents here:
    Public inMTO_Run_StartFromBoot As Intent
    Public inMTO_Run_StartFromMain, inMTO_Run_EndFromMain As Intent
       Public nNotify As Notification
End Sub
' Initialize settings
Sub Setup
    ' Initialize the Intents
    inMTO_Run_StartFromMain.Initialize("StartFromMain", "")
    inMTO_Run_StartFromMain.SetComponent("tbci.movie_timeout/.mto_run")
    inMTO_Run_EndFromMain.Initialize("EndFromMain", "")
    inMTO_Run_EndFromMain.SetComponent("tbci.movie_timeout/.mto_run")

The service start and service cancel is called from another service called MTO_Run:

B4X:
Sub Service_Start (StartingIntent As Intent)
 
Dim ltoe As Long
ltoe = MTO.ReadValue("TimeOutEnd")
    Select sAction
        Case "StartFromMain"
            ' This a request for an immediate "manual" TimeOut start, with a TimeOut End scheduled at TimeOutEnd
            StartTimeOut("User", DateTime.now)
            ' Set Schedule to end the TimeOut
            StartServiceAtExact(MTO.inMTO_Run_EndFromMain, ltoe, True)
        Case "EndFromMain"
             ' Cancel any schedules from Main
            CancelScheduledService(MTO.inMTO_Run_EndFromMain)
    End Select
    StopService("")
End Sub
This was working, but under version 8.0, the StartServiceAtExact fails with the following logs:
B4X:
*** Service (mto_run) Create ***
** Service (mto_run) Start **
MTO_Run Service started by StartFromMain
Starting Timeout at 27/03/18 10:48 AM by User
Error occurred on line: 68 (MTO_Run)
java.lang.ClassNotFoundException: tbci.movie_timeout.mto.inmto_run_endfrommain
    at java.lang.Class.classForName(Native Method)
    at java.lang.Class.forName(Class.java:251)
    at java.lang.Class.forName(Class.java:216)
    at anywheresoftware.b4a.keywords.Common.getComponentClass(Common.java:946)
    at anywheresoftware.b4a.keywords.Common.createPendingIntentForAlarmManager(Common.java:925)
    at anywheresoftware.b4a.keywords.Common.StartServiceAtExact(Common.java:916)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:755)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:345)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
    at tbci.movie_timeout.mto_run.handleStart(mto_run.java:100)
    at tbci.movie_timeout.mto_run.access$000(mto_run.java:8)
    at tbci.movie_timeout.mto_run$1.run(mto_run.java:71)
    at anywheresoftware.b4a.objects.ServiceHelper$StarterHelper.onStartCommand(ServiceHelper.java:221)
    at tbci.movie_timeout.mto_run.onStartCommand(mto_run.java:69)
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2889)
    at android.app.ActivityThread.access$2200(ActivityThread.java:169)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1377)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:146)
    at android.app.ActivityThread.main(ActivityThread.java:5487)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoClassDefFoundError: tbci/movie_timeout/mto/inmto_run_endfrommain
    ... 31 more
The CancelScheduledService code fails with these logs:

B4X:
Error occurred on line: 78 (MTO_Run)
java.lang.NullPointerException
    at android.content.ComponentName.<init>(ComponentName.java:78)
    at android.content.Intent.<init>(Intent.java:4334)
    at anywheresoftware.b4a.keywords.Common.createPendingIntentForAlarmManager(Common.java:925)
    at anywheresoftware.b4a.keywords.Common.CancelScheduledService(Common.java:934)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:755)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:345)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
    at tbci.movie_timeout.mto_run.handleStart(mto_run.java:100)
    at tbci.movie_timeout.mto_run.access$000(mto_run.java:8)
    at tbci.movie_timeout.mto_run$1.run(mto_run.java:71)
    at anywheresoftware.b4a.objects.ServiceHelper$StarterHelper.onStartCommand(ServiceHelper.java:221)
    at tbci.movie_timeout.mto_run.onStartCommand(mto_run.java:69)
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2889)
    at android.app.ActivityThread.access$2200(ActivityThread.java:169)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1377)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:146)
    at android.app.ActivityThread.main(ActivityThread.java:5487)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
    at dalvik.system.NativeStart.main(Native Method)

Looking for ideas on what is happening - what has changed that prevents this code from working now when it was working in previous versions. Not sure if it is specifically B4A v8 where the change occurred, I don't recompile code after every release of B4A.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Your code will not work. StartServiceAt expects a service, not an intent.
StartServiceAt implementation was changed in v8.0 and your code which passes an intent instead of a service will not work.
Note that the new implementation is supported by targetSdkVersion 26 and is more reliable on all devices.
One possible way to implement a similar logic is by creating two threads.
Though it will probably be simpler to store the additional parameter in KVS or a standard file.
 
Upvote 0
Top