Autorun daily at specific time... Help?

redincali

Member
Hello all. This is my first app for android, coming from VB for windows. So first I'd like to say thank you for basic4android, its amazing. Secondly, I'm having an issue with making my app run daily at a specific time. Please help me :)

What I have it do is when the user sets the time, it calculates ticks for the hours and minutes selected. It also checks if the time set is am or pm and adds 12hrs if its pm, but only if its not 12pm (noon). This is my code for that..

(in an activity)
B4X:
Dim a,b,c As Long
If sAmPm.SelectedItem = "PM" Then
   If sHour.SelectedItem = "12" Then
      a = sHour.SelectedItem * DateTime.TicksPerHour
      b = sMinute.SelectedItem * DateTime.TicksPerMinute
      c = a + b
   Else
      a = (sHour.SelectedItem + 12)* DateTime.TicksPerHour
      b = sMinute.SelectedItem * DateTime.TicksPerMinute
      c = a + b
   End If
Else
   If sHour.SelectedIndex = 12 Then
      b = sMinute.SelectedItem * DateTime.TicksPerMinute
      c = b
   Else
      a = sHour.SelectedItem * DateTime.TicksPerHour
      b = sMinute.SelectedItem * DateTime.TicksPerMinute
      c = a + b
   End If
End If

After that, it saves the ticks calculated to a key called "aTime" for the AutoRun service to read from.



Now, in my AutoRun service I have this...

B4X:
Dim cTime As Long
Dim n As Notification 
Dim nPhone As PhoneWakeState
Dim aN As String

If StateManager.GetSetting("aRun") = "False" Then Return 'Determines if autorun is turned on

cTime = StateManager.GetSetting("aTime") 'Gets tick count for autorun time
cTime = cTime + DateTime.DateParse(DateTime.Date(DateTime.Now))

StartServiceAt(Me,cTime,True)

If StartingIntent.HasExtra("android.intent.extra.ALARM_COUNT") Then
   isAuto = True 'for the activity to know its being ran automatically, not by user
   nPhone.KeepAlive(True)
   nPhone.PartialLock 
   StartActivity(Main)
   CallSub(Main,"AutoRunner")
   aN = StateManager.GetSetting2("aNote","False") 'checks if user wanted notification when autorun occurs
   If aN = "True" Then
      n.Initialize
      n.Icon = "icon"
      n.SetInfo("Auto Bing Rewards" , "Auto-Search Initiated...", Main)
      n.AutoCancel = True
      n.Notify(1)
   End If
End If

The only way I was able to have it work was by adding a check on a boolean I named "RunOnce". It would be false, then after an autorun it would be true. So I can get it to autorun once, but not any more after. I removed it and now after the time is met, autorun occurs but continues reoccuring loop until i force the app to stop running or I set the time to anything higher than the current time. Also if my time is, for example, 5:12pm and i set it to autorun at 5:11pm or any time earlier, it autoruns and then gets into the loop as well.

I'm still attempting to fix this myself but if i can get any help, it would be appreciated. If more of my code is needed to help, I'll share it as well. Thanks all.
 
Top