Android Question Multiple alarm or notification

G-ShadoW

Active Member
Licensed User
Longtime User
I want to write in txt file time and date for multiple alarm's, then using service ( started at boot ) I will compare all lines in txt file, find closest date time for alarm and reschedule service, but...

What should I save in txt file, datetime ticks or normal date time format eg. (22/07/2015 13:00:00 )

here is the project:

from Log's I have this info:

B4X:
today: 1436652000000
td.hour: 15
td.minute: 0
1436706000000
DateTime.Now: 1436620042765
Starting date and time: 12.07.2015 15:00:00
Alarm will start after: 23 hour(s) and 52 minute(s)
StartServiceAt: 1436705962775
Alarm will start at: 12.07.2015 15:00:00
Alarm service - canceled.
 

Attachments

  • ok_alarm.zip
    9 KB · Views: 229

G-ShadoW

Active Member
Licensed User
Longtime User
Ok, for now, I can add serveral time-date in txt file and and in 99% alarm will work, it will find closest alarm to trigger
but,
when alarm start's it will stay on it and it play notification until you delete it ( it will not search for next time-date in txt file ) <- need fix

So I need help,
when notification/alarm start, I need only 1 notification and then search for next alarm and schedule it ( but not to delete it from txt file
coz I want to save it as remminder).

Also, I cant get any info from service using callsubdelayed...

Service1

B4X:
#Region Module Attributes
    #StartAtBoot: true
#End Region

'Service module
Sub Process_Globals

    Dim MyList As List
    Dim FilePath As String =File.DirRootExternal & "/AlarmS/"
    Dim FileName As String ="alarm.txt"
    Dim datum As String
    Dim sat As String
    Dim timer1 As Timer
    Dim top As String
End Sub
Sub Service_Create
 

End Sub

Sub Service_Start

     timer1.Initialize("timer1",20000)
     timer1.Enabled=True

End Sub

Sub Service_Destroy
    StopService("")     'Stop this service
    CancelScheduledService("")   'Cancel this service
End Sub

Sub timer1_tick
    MyList.Initialize
  
    DateTime.DateFormat = "dd.MM.yyyy"
    DateTime.TimeFormat = "HH:mm:ss"

    Dim distanceNew As Float
    Dim distanceShortest As Float = Power(10, 38)
    Dim intIndex As Int

    MyList.Clear

    Dim reader As TextReader      
    reader.Initialize(File.OpenInput(FilePath,FileName))
    Dim line As String
    line = reader.ReadLine
    Do While line <> Null
          MyList.add(line)
          line = reader.ReadLine
    Loop              
    reader.Close
    'Dim x As String
    Dim y As String
   ' Dim b As String
  
    For i=0 To MyList.Size-1
      Dim line As String =MyList.Get(i)
  
        Dim arrLine() As String = Regex.Split(";",line)
       ' x = arrLine(0) ' notification
        y = arrLine(1) ' date and time
        'b = arrLine(2) 'not important
      
        Dim serverTime As Long = DateTime.DateParse(y)
        Log("servertime: " & serverTime)

        Log("timenow: "& DateTime.Now)
      
        distanceNew = Abs(serverTime) - Abs(DateTime.now)
        Log("distancenew: " & distanceNew)
        Log("distanceshortest: " & distanceShortest)
       
        If distanceNew < distanceShortest Then
            distanceShortest = distanceNew
            intIndex = i
            Log("Closest match found... " & distanceShortest & " at index position: " & intIndex)
          
          
        End If
    Next
    If MyList.Size=0 Then
    timer1.Enabled=False
    StopService(Me)
    StopService(AlarmService2)
    CancelScheduledService(Me)
    CancelScheduledService(AlarmService2)
    Return
    End If
   
  
    Try
     Dim line As String = MyList.Get(intIndex)
    'Dim SplitLine() As String = Regex.Split("-", line)
    Dim SplitLine() As String = Regex.Split(";", line.Replace("-", ";"))
    datum= SplitLine(1)
    sat=SplitLine(2)
    Catch
    Log("Error: " & LastException.Message)
    StopService(Me)
    CancelScheduledService(Me)
    ToastMessageShow("Error, service is off",True)
    End Try
  
    Log("datum: " & datum & " <-> sat: " & sat)
  
    If datum="" Then
    Return
    End If
  
    If sat="" Then
    Return
    End If

    StartServiceAt(AlarmService2, DateTime.DateTimeParse(datum,sat),True)
    Log("Next alarm will be set for: " & datum & " - " & sat)
    top=datum & " - " & sat

End Sub

Sub Info
    top=datum & " - " & sat
End Sub

Service2

B4X:
#Region  Service Attributes
    #StartAtBoot: 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.
     Dim noti As Notification

End Sub
Sub Service_Create
  
   
End Sub

Sub Service_Start (StartingIntent As Intent)

noti.Initialize
        noti.Light = True
        noti.Vibrate = True
        noti.OnGoingEvent = False
        noti.Sound = True
        noti.Icon = "icon"
        noti.SetInfo("Reminder","Something to do today!",Alarm)
        noti.AutoCancel=True
        noti.Notify(1)
       
   
    ToastMessageShow("Please read the remminder and delete it from list!", False)
    StopService(Me)
    CancelScheduledService(Me)
End Sub

Sub Service_Destroy
    StopService("")     'Stop this service
    CancelScheduledService("")   'Cancel this service
End Sub

Activity

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

Sub Process_Globals

     Dim noti As Notification
     Dim timer1 As Timer
End Sub

Sub Globals

    Private Panel1 As Panel
    Private lstAlarms As ListView
    Private lblTitle6 As Label
    Private Panel2 As Panel
    Private Panel3 As Panel
    Private ImageView1 As ImageView
    Private lblAlarm As Label
    Private EditText1 As EditText
    Private EditText2 As EditText
    Private EditText3 As EditText
    Private Button2 As Button
    Private lblCancelAlarm As Label
    Private ImageView2 As ImageView
    Dim MyList As List
    Dim FilePath As String =File.DirRootExternal & "/AlarmS/"
    Dim FileName As String ="alarm.txt"
    Dim lblNextAlarm As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)

    Activity.LoadLayout("frmAlarm")
    MyList.Initialize

   lstAlarms.TwoLinesAndBitmap.Label.TextColor=Colors.Blue
    lstAlarms.TwoLinesAndBitmap.Label.TextSize=22
    lstAlarms.TwoLinesAndBitmap.SecondLabel.TextColor=Colors.Black
    lstAlarms.TwoLinesAndBitmap.SecondLabel.TextSize=13
    lstAlarms.TwoLinesAndBitmap.Label.Gravity = Gravity.LEFT
    lstAlarms.TwoLinesAndBitmap.SecondLabel.Visible = True
   
    Dim r As Reflector
   If r.GetStaticField("android.os.Build$VERSION", "SDK_INT") >= 10 Then
      r.Target = lstAlarms
      r.RunMethod4("setOverscrollFooter", Array As Object(Null), _
         Array As String("android.graphics.drawable.Drawable"))
   End If

   
    ReadTextFile
    timer1.Initialize("timer1", 5000)
    timer1.Enabled=True

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub lstAlarms_ItemClick (Position As Int, Value As Object)
    ToastMessageShow("Value: " & Value & " Position: " & Position,True)
End Sub
Sub lstAlarms_ItemLongClick (Position As Int, Value As Object)
  
     StopService(AlarmService)
     CancelScheduledService(AlarmService)
   
     Dim Answ As Int
     Dim txt As String

        txt = "Do you want to delete alarm?"
        Answ = Msgbox2(txt, "Info", "Yes", "", "No", Null) ' MessageBox
       
        If Answ = DialogResponse.POSITIVE Then   
        lstAlarms.RemoveAt(Position)
        MyList.Get(Position)
        MyList.RemoveAt(Position)
        File.WriteList(File.DirRootExternal & "/AlarmS/", "alarm.txt",MyList)
        StartService(AlarmService)
        Else
        StartService(AlarmService)
        End If

     Log(Position)
     Log(Value)
    

End Sub
Sub ImageView1_Click
   
    StopService(AlarmService)
    StopService(AlarmService2)

    Dim td As TimeDialog
    Dim today As Long
   
    DateTime.DateFormat = "dd.MM.yyyy"

    Dim dd As DateDialog
    dd.ShowCalendar=False
    dd.Year=DateTime.GetYear(DateTime.Now)
    dd.Month=DateTime.GetMonth(DateTime.Now)
    dd.DayOfMonth=DateTime.GetDayOfMonth(DateTime.Now)
   
   
    td.Hour= DateTime.GetHour(DateTime.Now)
    td.Minute=DateTime.GetMinute(DateTime.Now)
    td.Is24Hours=True
   
    Dim bb As InputDialog
    
     If bb.Show("Enter small notification...", "Alarm", "OK", "", "No",Null) = DialogResponse.POSITIVE Then
     Log(bb.Input)
    
     If bb.Input="" Then
     Msgbox("There is no any notification info!","Info")
     StartService(AlarmService)
     Return
     End If
    
     Else
     StartService(AlarmService)
     Return
    
     End If
   
    If dd.Show("NOTIFICATION","Select date","OK","","Cancel",Null)=DialogResponse.POSITIVE Then
    today=dd.DateTicks
    NumberFormat(dd.Month,2,0)
    EditText1.Text=NumberFormat (dd.DayOfMonth,2,0) & "." & NumberFormat(dd.Month,2,0) & "." & dd.Year
    Log("today: " & today)
   
    Else
    StartService(AlarmService)
    Return
   
    End If
   
    If td.Show("Select time", "ALARM", "Ok", "Cancel", "", Null) = DialogResponse.POSITIVE Then

        EditText2.Text=NumberFormat(td.Hour,2,0) & ":" & NumberFormat(td.Minute,2,0) & ":" & "00"
       
        If DateTime.Now > DateTime.DateTimeParse(EditText1.Text,EditText2.Text) Then
        Msgbox("Tima have already past!", "Info")
        StartService(AlarmService)
        Return
        End If
       
    End If
  
   ToastMessageShow("Next alarm set for : " & EditText1.Text & " - " & EditText2.Text,True)
   EditText3.Text= bb.Input & ";" & EditText1.Text & "-" & EditText2.text & ";" & EditText1.text
   lstAlarms.AddTwoLinesAndBitmap2(bb.Input,EditText1.Text & "-" & EditText2.text,LoadBitmap(File.DirAssets,"bee.png"),EditText3.text)
  
   MyList.Add(EditText3.Text)
  
   Try
   File.WriteList(File.DirRootExternal & "/AlarmS/","alarm.txt",MyList)
   Catch
   Log("Error: " & LastException.Message)
   ToastMessageShow("Error: " & LastException.Message,True)
   End Try
  
   Button2_Click
End Sub
Sub Button2_Click
     DateTime.DateFormat = "dd.MM.yyyy"
     DateTime.TimeFormat = "HH:mm:ss"
     StartService(AlarmService)
     'StartServiceAt(AlarmService, DateTime.DateTimeParse(EditText1.text,EditText2.text),True)
End Sub
Sub ImageView2_Click
        noti.Initialize
        noti.Cancel(1)
        StopService(AlarmService)
        CancelScheduledService(AlarmService)
        StopService(AlarmService2)
        CancelScheduledService(AlarmService2)
        Log("Alarm service - canceled.")
       
        ToastMessageShow("All timers are off!",True)
End Sub

Sub ReadTextFile
   
    If MyList.IsInitialized=False Then
    MyList.Initialize
    End If
   
    MyList.Clear
    lstAlarms.Clear
   
    Dim reader As TextReader       
    reader.Initialize(File.OpenInput(FilePath,FileName))
    Dim line As String
    line = reader.ReadLine
    Do While line <> Null
          MyList.add(line)
          line = reader.ReadLine
    Loop               
    reader.Close 
    Dim x As String
    Dim y As String
    Dim b As String

    For i=0 To MyList.Size-1
      Dim line As String =MyList.Get(i)
   
        Dim arrLine() As String = Regex.Split(";",line)
        x = arrLine(0)
        y = arrLine(1)
        b = arrLine(2)
       Log("List: " & x & " - " & y)

        lstAlarms.AddTwoLinesAndBitmap2(x,y,LoadBitmap(File.DirAssets,"bee.png"),b)
       
    Next

End Sub

Sub timer1_tick
    CallSubDelayed(AlarmService,"Info")
   
    If IsPaused(AlarmService) = True Then
    'lblNextAlarm.Text="All alarms are ON!"
    Else
    'lblNextAlarm.Text="-=|||=-"
    End If

End Sub

Sub GetResult(Result As String)
   lblNextAlarm.Text=  Result
   Log("Rezultat callsubdelayed: " & Result)
End Sub
 
Upvote 0
Top