Android Question using SetExactAndAllowWhileIdle without the need for notifications

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Everyone,

I would like to use SetExactAndAllowWhileIdle without needing to also use notifications. Everything goes well with SetExactAndAllowWhileIdle such as rescheduling the startup of a service and it starts up very close to being on time.

The service is sticky:

B4X:
   #StartCommandReturnValue: android.app.Service.START_STICKY

It's also started in the foreground in Service_Create:

B4X:
    Service.StartForeground( 8, nNotify)

Elsewhere in a sub routine I have this notification:

B4X:
        nNotify.SetInfo("My App", "This is a test notification.", "")
        nNotify.Notify(3)

If I remove the above line of code the service will start up again on the scheduled time since I have SetExactAndAllowWhileIdle in Service_Create, but will not start again ignoring the new scheduled time for SetExactAndAllowWhileIdle that I have in other parts of the app.

Is there a way to get SetExactAndAllowWhileIdle to work without the notification?

Thanks
 

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Erel,

If the sticky services are being removed, what coding can I use in place of them to reschedule waking up my service and will I still need to use notifications? Some of my users don't want the notifications. Will I still be able to use SetExactAndAllowWhileIdle ?

Thanks.
 
Last edited:
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Sticky services have nothing to do with scheduled services.

What is your service doing once it starts?

Hi Erel,

When the app is run for the first time, I start the activity:

B4X:
StartService(ServiceTimeForPrayer)

It calls SetExactAndAllowWhileIdle so the service will wake up at a given time:

B4X:
    If DateTime.Now >= lngFajrTics And DateTime.Now < lngSunriseTics Then
        SetExactAndAllowWhileIdle(lngSunriseTics -180000, "ServiceTimeForPrayer")
    End If

    If DateTime.Now >= lngSunriseTics And DateTime.Now < lngDhuhrTics Then
        SetExactAndAllowWhileIdle(lngDhuhrTics -180000, "ServiceTimeForPrayer")
    End If

    If DateTime.Now >= lngDhuhrTics And DateTime.Now < lngAsrTics Then
        SetExactAndAllowWhileIdle(lngAsrTics -180000, "ServiceTimeForPrayer")
    End If

    If DateTime.Now >= lngAsrTics And DateTime.Now  < lngMagribTics Then
        SetExactAndAllowWhileIdle(lngMagribTics -180000, "ServiceTimeForPrayer")
    End If

    If DateTime.Now >= lngMagribTics And DateTime.Now < lngIshaTics Then
        SetExactAndAllowWhileIdle(lngIshaTics -180000, "ServiceTimeForPrayer")
    End If
    
    If DateTime.Now >= lngIshaTics Then

        ' For Fajr, this service needs to be set so it wakes up tomorrow since it's isha now.
        '------------------------------------------------------------------------------------
        pPeriodToAdd.Days = 1
        lngFajrTics = DateUtils.AddPeriod(DateTime.Now, pPeriodToAdd)
        
        SetExactAndAllowWhileIdle(lngFajrTics -180000, "ServiceTimeForPrayer")       
    End If
End Sub

Once the service starts up on the scheduled time a timer is started at 1 second ticks. A sound file is played at a given time and the timer is disabled when the player finishes playing the sound file. Also another call to SetExactAndAllowWhileIdle reschedules the service to wake up again at another time.

This all works well as long is this statement is placed inside the timer tick:

B4X:
nNotify.Notify(0)

If I remove that statement, the service will not wake up at the scheduled time. I don't know what the connection to notifications and SetExactAndAllowWhileIdle is but removing the notification call seems to break the processing.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
You should make the service a foreground service and acquire a partial lock. Otherwise Android will very quickly kill the service.

Hi Erel,

Can you show a coding sample of acquiring a partial lock? I could not locate one by searching.

Thanks.
 
Upvote 0

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Erel,

Found a coding sample. :)

Here's what I used.

B4X:
Dim phWakeState As PhoneWakeState

phWakeState.PartialLock
 
Upvote 0
Top