Android Question How to Stop Starter Service from error

Discussion in 'Android Questions' started by YIM bunchhat, Jul 23, 2019.

  1. YIM bunchhat

    YIM bunchhat Active Member Licensed User

    Hello, I have one problem. I use Starter Service for looking my Bluetooth connection while it disconnect. it work ok.
    but problem is when I close app it got error below:

    Code:
    The Starter service should never be started from a receiver.
    *** 
    Service (starter) Create ***
    ** 
    Service (starter) Start **
    starter_service_start (java line: 
    163)
    java.lang.IllegalStateException: Interval must be larger than 
    0.
        at anywheresoftware.b4a.objects.Timer.setEnabled(
    Timer.java:79)
        at b4a.gsmmodem.starter._service_start(starter.java:
    163)
        at java.lang.reflect.Method.invoke(Native Method)
        at anywheresoftware.b4a.BA.raiseEvent2(BA.java:
    186)
        at anywheresoftware.b4a.BA.raiseEvent(BA.java:
    166)
        at b4a.gsmmodem.starter.handleStart(starter.java:
    100)
        at b4a.gsmmodem.starter.access$
    000(starter.java:8)
        at b4a.gsmmodem.starter$
    1.run(starter.java:71)
        at anywheresoftware.b4a.objects.ServiceHelper$StarterHelper.onStartCommand(ServiceHelper.java:
    221)
        at b4a.gsmmodem.starter.onStartCommand(starter.java:
    69)
        at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:
    3609)
        at android.app.ActivityThread.-wrap20(Unknown Source:
    0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:
    1717)
        at android.os.Handler.dispatchMessage(Handler.java:
    105)
        at android.os.Looper.loop(Looper.java:
    164)
        at android.app.ActivityThread.main(ActivityThread.java:
    6710)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:
    240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:
    770)
    java.lang.RuntimeException: Unable 
    to start service b4a.gsmmodem.starter@608448a with Intent { cmp=b4a.gsmmodem/.starter (has extras) }: java.lang.RuntimeException: java.lang.IllegalStateException: Interval must be larger than 0.
        at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3626)
        at android.app.ActivityThread.-wrap20(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1717)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6710)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
    Caused by: java.lang.RuntimeException: java.lang.IllegalStateException: Interval must be larger than 0.
        at anywheresoftware.b4a.BA.raiseEvent2(BA.java:223)
        at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
        at b4a.gsmmodem.starter.handleStart(starter.java:100)
        at b4a.gsmmodem.starter.access$000(starter.java:8)
        at b4a.gsmmodem.starter$1.run(starter.java:71)
        at anywheresoftware.b4a.objects.ServiceHelper$StarterHelper.onStartCommand(ServiceHelper.java:221)
        at b4a.gsmmodem.starter.onStartCommand(starter.java:69)
        at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3609)
     
  2. MarkusR

    MarkusR Well-Known Member Licensed User

    sounds like u use a timer, you forgot to set the Interval value?
    what is in line "starter_service_start (java line: 163)" ?
     
  3. YIM bunchhat

    YIM bunchhat Active Member Licensed User

    Hello MarkusR here is my code:
    Code:
    #Region  Project Attributes
        
    #ApplicationLabel: GSM Modem Solar
        
    #VersionCode: 1
        
    #VersionName:
        
    'SupportedOrientations possible values: unspecified, landscape or portrait.
        #SupportedOrientations: portrait
        
    #CanInstallToExternalStorage: False
        
    #BridgeLogger: True
    #End Region

    #Region  Activity Attributes
        
    #FullScreen: False
        
    #IncludeTitle: False
    #End Region

    Sub Process_Globals
        
    'These global variables will be declared once when the application starts.
        'These variables can be accessed from all modules.
        Private bt As Serial
        
    Private ast As AsyncStreams
        
    Public timer As Timer
    End Sub

    Sub Globals
        
    'These global variables will be redeclared each time the activity is created.
        'These variables can only be accessed from this module.

        
    Private btnback As Button
        
    Private lbldis As Label
        
    Private imgblue As ImageView
        
    Private imgclock As ImageView
        
    Private imgsetting As ImageView
        
    Private imgsolar As ImageView
        
    Private imgwire As ImageView
        
    Private Label2 As Label
        
    Private Label3 As Label
        
    Private Label4 As Label
        
    Private Label5 As Label
        
    Private pairestr As String
        
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
        
    'Do not forget to load the layout file created with the visual designer. For example:
        Activity.LoadLayout("solar")
        bt.Initialize(
    "bt")
        
    StopService(Starter)
        
    timer.Initialize("timer",5000)
    End Sub

    Sub Activity_Resume

    End Sub

    Sub Activity_Pause (UserClosed As Boolean)
        
    If UserClosed Then
            bt.Disconnect
            
    timer.Enabled = False
            
    StopService(Starter)
        
    End If
    End Sub

    Sub timer_Tick
        
    timer.Enabled = False
        
    If Starter.connected = False Then
            
    ProgressDialogShow2("Reconnecting to Bluetooth....",False)
            
    Do While Starter.connected = False
                bt.Connect(pairestr)
                Sleep(
    2000)
            
    Loop
        
    End If
    End Sub

    Sub bt_Connected (Success As Boolean)
        
    If Success Then
            ast.Initialize(bt.InputStream,bt.OutputStream,
    "ast")
            
    StartService(Starter)
            
    ProgressDialogHide
        
    End If
    End Sub

    Sub visiblview(stat As Boolean)
        
    If stat = True Then
            imgblue.Visible = 
    False
            imgclock.Visible = 
    False
            imgsetting.Visible = 
    False
            imgsolar.Visible = 
    False
            imgwire.Visible = 
    False
            Label2.Visible = 
    False
            Label3.Visible = 
    False
            Label4.Visible = 
    False
            Label5.Visible = 
    False
            lbldis.Visible = 
    True
            btnback.Visible = 
    True
        
    Else
            imgblue.Visible = 
    True
            imgclock.Visible = 
    True
            imgsetting.Visible = 
    True
            imgsolar.Visible = 
    True
            imgwire.Visible = 
    True
            Label2.Visible = 
    True
            Label3.Visible = 
    True
            Label4.Visible = 
    True
            Label5.Visible = 
    True
            lbldis.Visible = 
    False
            btnback.Visible = 
    False
        
    End If
    End Sub

    Sub btnback_Click
        visiblview(
    False)
    End Sub

    Sub imgsolar_Click
        visiblview(
    True)
    End Sub

    Sub imgsetting_Click
        
    End Sub

    Sub imgclock_Click
        
    End Sub

    Sub imgblue_Click
        
    Dim PairedDevices As Map
        PairedDevices = bt.GetPairedDevices
        
    Dim l As List
        l.Initialize
        
    For i = 0 To PairedDevices.Size - 1
            l.Add(PairedDevices.GetKeyAt(i))
        
    Next
        
    Dim res As Int
        res = 
    InputList(l, "Choose device", -1'show list with paired devices
        If res <> DialogResponse.CANCEL Then
            bt.Connect(PairedDevices.Get(l.Get(res))) 
    'convert the name to mac address
            pairestr = PairedDevices.Get(l.Get(res))
        
    End If
    End Sub
    And here is Service Starter
    Code:
    #Region  Service Attributes
        
    #StartAtBoot: False
        
    #ExcludeFromLibrary: True
    #End Region

    Sub Process_Globals
        
    'These global variables will be declared once when the application starts.
        'These variables can be accessed from all modules.
        Public connected As Boolean
    End Sub

    Sub Service_Create
        
    'This is the program entry point.
        'This is a good place to load resources that are not specific to a single activity.

    End Sub

    Sub Service_Start (StartingIntent As Intent)
        
    If StartingIntent.Action = "android.bluetooth.device.action.ACL_CONNECTED" Then
            
    ToastMessageShow("Connected to device",True)
            connected = 
    True
            Main.timer.Enabled = 
    False
        
    Else If StartingIntent.Action = "android.bluetooth.device.action.ACL_DISCONNECTED" Then
            connected = 
    False
            Main.timer.Enabled = 
    True
        
    End If

    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 StringAs Boolean
        
    Return True
    End Sub

    Sub Service_Destroy

    End Sub
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Several mistakes in this code.
    I will not list them all. Only a single tip:
    Public process globals should be declared in the starter service and be initialized in Service_Create.
     
  5. MarkusR

    MarkusR Well-Known Member Licensed User

    i would remove this StopService(Starter)/StartService(Starter)
    let the timer enabled because u already check If connected = False Then reconnect
    i would move all this connect stuff into a class with its functionality.
    from class you can call a sub in activity to show/hide the connect ProgressDialog.
     
  6. YIM bunchhat

    YIM bunchhat Active Member Licensed User

    hello when I keep timer enable, during Progressdialogshow it seem refresh or blink my app
     
  7. MarkusR

    MarkusR Well-Known Member Licensed User

    u can use a boolean value flag if dialog should get visible or hide. so it will done once.
     
    Last edited: Jul 23, 2019
  8. YIM bunchhat

    YIM bunchhat Active Member Licensed User

    hello now it work thank you
     
    MarkusR likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice