Android Question How to StartActivity in specific time ?

mr.gedo

Member
Hello

i have 2 label

label1 = "05:30"
label2 = "20:30"

i need to know how i can StartActivity automaticly when time = label like :

if time = label1 then StartActivity("1")
if time = label2 then StartActivity("2")

but i need this in background service when app is closed
 

mr.gedo

Member
Search for StartServiceAt.


i found this code here

B4X:
Sub Service_Start (StartingIntent As Intent)
'Service.StopAutomaticForeground 'Starter service can start in the foreground state in some edge cases.

    StartServiceAt(Me, NextTimeInstance(05, 30) ,True)

End Sub


Sub NextTimeInstance (Hours As Int, Minutes As Int) As Long
    Dim today As Long = DateTime.Now
    today = DateUtils.SetDateAndTime(DateTime.GetYear(today), DateTime.GetMonth(today), _
     DateTime.GetDayOfMonth(today), Hours, Minutes, 0)
    If today < DateTime.Now Then
        Dim p As Period
        p.Days = 1
        Dim tomorrow As Long = DateUtils.AddPeriod(today, p)
        Return tomorrow
    Else
        Return today
    End If
End Sub


But actually i don't know how to use it
I want to start the service every day at the time specified in my label becuse the user can change the time and save new one
 

Attachments

  • StartServiceAt.zip
    12.7 KB · Views: 105
Upvote 0

mr.gedo

Member
Update :

i add another service module and tried this code throughout the day and work good but
i want to start the service every day, once in the morning and once in the evening, depending on the time the user chooses in setting page
Also, I do not want the permanent notification to appear, what should I do?

B4X:
    StartServiceAt(Me, NextTimeInstance(18, 06) ,True)
 StartActivity(act1)

Please help

Best Regards
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You will need to do some reading.
Also, I do not want the permanent notification to appear, what should I do?
The permanent notification is related to the automatic foreground mode: https://www.b4x.com/search?query=automatic+foreground+mode

i want to start the service every day, once in the morning and once in the evening, depending on the time the user chooses in setting page
This is exactly the purpose of NextTimeInstance. It calculates the next scheduled time.
 
Upvote 0

mr.gedo

Member
You will need to do some reading.

The permanent notification is related to the automatic foreground mode: https://www.b4x.com/search?query=automatic+foreground+mode


This is exactly the purpose of NextTimeInstance. It calculates the next scheduled time.

Thank you Erel
The permanent notification issue has been resolved by add this code

B4X:
    Sleep(0)
Service.StopAutomaticForeground

but i still have 2 problems here
1- i don't want to start the service when I open the app every time, i just want to start service when time = the specific time in the label
2- i don't know how to schedule two time and link with my label
i try to schedule 2 time like :

B4X:
    StartServiceAt(Me, NextTimeInstance(24, 01) ,True)
    StartActivity(act1)
    StartServiceAt(Me, NextTimeInstance(01, 10) ,True)
    StartActivity(act2)

but this skiped the first service

i will use Keyvaluestore to save my time setting so i'm thinking about saving hours and minutes separately like this

B4X:
StartServiceAt(Me, NextTimeInstance(KVS.GetDefault("H-PM" , "21"), KVS.GetDefault("M-PM" , "15")) ,True)

so when user change the time and save the new time will update automaticly in keyvaluestore

Is this a correct or not?

Best Regards
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
i don't want to start the service when I open the app every time, i just want to start service when time = the specific time in the label
You don't need to start any service when the app is opened. Only call StartServiceAt.

You cannot make multiple schedules. You need to find the closest time instance and schedule that one. When that time arrives, you should schedule the next one.
 
Upvote 0
Top