Android Question my app crashes when it resumes

Albi

Active Member
Licensed User
It crashes when resuming when I compile in release mode, but it doesn't crash when I run it in debug(rapid) mode, so I can't get much info on where the problem lies.
It has a main module, a service module and a code module for kvs.

All it does is set an alarm to go off every 30 seconds. If you change the spinner, the notification info in the alarm updates, but the time of the alarm remains the same.

Code is below:

Main module:

B4X:
Sub Process_Globals
    Dim alarmTicks As Long 'ticks till next alarm
End Sub

Sub Globals
    Dim btnTime, btnCancel As Button 'sets alarm, cancels alarm
    Dim spnQuotes As Spinner
    Dim p As Period :p.seconds = 30
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("page1")
    initKVS.initalizingData
    spnQuotes.AddAll(Array As String("Title 1", "Title 2", "Title 3", "Title 4", "Title 5", "Title 6"))
End Sub

Sub Activity_Resume
    alarmService.noti.Cancel(1)
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub btnCancel_Click
    StopService(alarmService)
    CancelScheduledService(alarmService)
End Sub

Sub btnTime_Click
    alarmTicks = DateTime.Now+(30*1000)
    StartServiceAt(alarmService, alarmTicks, True)
    ToastMessageShow("btn alarm set for " & DateTime.Time(alarmTicks),False)
End Sub

'if you change the quote then the alarm, via the service module, needs to be updated to reflect new notification text
'relies on the quote number, which uses the KVS
Sub spnQuotes_ItemClick (Position As Int, Value As Object)
    'ToastMessageShow(Position, False)
    initKVS.kvs.putsimple("quoteNumber", Position) 'this is used to display specific quote

    If alarmTicks > 0 Then 'only run this if an alarm has been set
        Do While alarmTicks < DateTime.Now 'make sure alarm is set in the future
            alarmTicks = DateUtils.AddPeriod(alarmTicks, p)
        Loop
        StartServiceAt(alarmService,alarmTicks,True)
    End If
    ToastMessageShow("spn alarm set for " & DateTime.Time(alarmTicks),False)
End Sub
Service Module:
B4X:
Sub Process_Globals
    Dim noti As Notification
    Dim quoteNumber As Int 'save this in kvs
    Dim quoteList As List
    Dim quoteTitle As List
    Dim AlarmTime As Long
End Sub
Sub Service_Create
    quoteList.Initialize
    quoteTitle.Initialize
    Quotes
    initKVS.initalizingData
    quoteNumber = initKVS.kvs.GetSimple("quoteNumber")
End Sub

Sub Service_Start (StartingIntent As Intent)
    noti.Initialize
    noti.Light = False
    noti.Vibrate = True
    noti.OnGoingEvent = False
    noti.Sound = True
    noti.Icon = "icon"
    noti.SetInfo(quoteTitle.Get(quoteNumber), quoteList.get(quoteNumber), Main)
    noti.Notify(1)
   
    Dim p As Period
    p.seconds = 30
    Do While AlarmTime < DateTime.Now 'make sure alarm is set in the future
        AlarmTime = DateUtils.AddPeriod(AlarmTime, p)
    Loop
    StartServiceAt("",AlarmTime,True)
    ToastMessageShow("service alarm set for " & DateTime.Time(AlarmTime),False)
End Sub

Sub Service_Destroy
    noti.Cancel(1)
    CancelScheduledService("")
End Sub

Sub Quotes
quoteList.Addall(Array As String("Quote 1", "Quote 2", "Quote 3", "Quote 4", "Quote 5", "Quote 6"))
quoteTitle.addAll(Array As String("Title 1", "Title 2", "Title 3", "Title 4", "Title 5", "Title 6"))
End Sub
kvs module
B4X:
'Code module
'Subs in this code module will be accessible from all modules.
Sub Process_Globals
    Dim kvs As KeyValueStore
End Sub

Sub initalizingData
    If kvs.IsInitialized = False Then
        kvs.Initialize(File.DirDefaultExternal, "mweData")
    End If
    If kvs.ContainsKey("quoteNumber")=False Then
        kvs.PutSimple("quoteNumber", 1)
    End If
End Sub
 

eurojam

Well-Known Member
Licensed User
Albi,
it is easier to test your app, when it is attached as zip (File->Export as zip) containing all files like layouts and so on...

best regards
stefan
 

Albi

Active Member
Licensed User
good point! attached now...

(edited the file as the designer didn't open before as i'd deleted an image to make it small enough to post!)
 

Attachments

Last edited:

eurojam

Well-Known Member
Licensed User
it works on my nexus, but it hangs after the first noti, because you will start an nearly infintiy loop because there is no initial value for your AlarmTime (it is 0) .... see my comments in your code:
B4X:
    noti.Initialize
    noti.Light = False
    noti.Vibrate = True
    noti.OnGoingEvent = False
    noti.Sound = True
    noti.Icon = "icon"
    noti.SetInfo(quoteTitle.Get(quoteNumber), quoteList.get(quoteNumber), Main)
    noti.Notify(1)
   
    Dim p As Period
    p.seconds = 30
   
    Do While AlarmTime < DateTime.Now 'AlarmTime is 0 and you have a very long loop
        Log ("Alarm " & AlarmTime & " now " & DateTime.now)
        AlarmTime = DateUtils.AddPeriod( AlarmTime, p)

    Loop
    Log("loop...end")
    StartServiceAt(  "",AlarmTime,True)  'here you call the service again in the service_start....?
    ToastMessageShow("service alarm set for " & DateTime.Time(AlarmTime),False)
 

Albi

Active Member
Licensed User
thanks! I was trying to work out where i'd gone wrong in the restarting of the service!!
 
Top