Android Question StartServiceAtExact not waking device

db0070

Active Member
Licensed User
Longtime User
When my application is running or has closed just a few minutes ago, then the StartServiceAtExact works. When the device is off and the service is set to run a few hours later, it does not wake the device. I am using android-23. I have tried this code first:
B4X:
StartServiceAtExact(alarms, NextTimeInstance(DateTime.GetHour(t), DateTime.GetMinute(t)), True)

And I have also tried this: -
B4X:
SetExactAndAllowWhileIdle(NextTimeInstance(DateTime.GetHour(t), DateTime.GetMinute(t)),"alarms")

Sub SetExactAndAllowWhileIdle (t As Long, ServiceName As String)
    Dim p As Phone
    If p.SdkVersion < 23 Then
        StartServiceAtExact(ServiceName, NextTimeInstance(DateTime.GetHour(t), DateTime.GetMinute(t)), True)
    Else
        Dim in As Intent
        in.Initialize("", "")
        in.SetComponent(Application.PackageName & "/." &  ServiceName.ToLowerCase)
        Dim jin As JavaObject = in
        jin.RunMethod("setAction", Array(Null))
        Dim ctxt As JavaObject
        ctxt.InitializeContext
        Dim am As JavaObject = ctxt.RunMethod("getSystemService", Array("alarm"))
        Dim pi As JavaObject
        pi = pi.InitializeStatic("android.app.PendingIntent").RunMethod("getService",  Array(ctxt, 1, in, 134217728))
        am.RunMethod("setExactAndAllowWhileIdle", Array(0, NextTimeInstance(DateTime.GetHour(t), DateTime.GetMinute(t)), pi))
    End If
End Sub
 

db0070

Active Member
Licensed User
Longtime User
I am installing it on the Galaxy Tab A Android Version 6.0.1 in release mode
This is the manifest editor:-
B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="14"/>
<supports-screens android:largeScreens="true" 
    android:normalScreens="true" 
    android:smallScreens="true" 
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'SetApplicationAttribute(android:theme, "@android:style/Theme.Holo")
'End of default text.
 
Last edited:
Upvote 0

db0070

Active Member
Licensed User
Longtime User
The more I search the web, the more I find this doze feature of Android is causing so many problems. I installed in release mode to my Sony phone API 15 and if the phone is off for a while, the alarm will not go off until I turn the phone on, 5 or even 10 minutes after the set alarm time! I see that others on the forum are having the same problem. Then I found on my Samsung Tablet running API 23 that the alarm goes off at 2 minutes past the alarm setting. I also find that the morning alarm will not go off. I have stripped off my code and left the bare minimum to test the SetExactAndAllowWhileIdle function. What am I doing wrong? When the Sony Phone (API 15) in built alarm clock is set for next morning at 05:23 and sure enough it goes off at 05:23 - so why does my code not behave like this? What is it that Sony's Alarm is doing is doing that I am not?
 

Attachments

  • TestAlarms.zip
    282 KB · Views: 238
Upvote 0

db0070

Active Member
Licensed User
Longtime User
In the Sony phone (running API 15) I cannot find the option to whitelist the app - this is the one where the service does not start until the phone is turned on. I am going to add PhoneWakeState to the service and see if this changes the behaviour on both phones.

On the Samsung (running API 23), the service does start without whitelisting the app, but it starts 2 minutes later. This means that when I publish my APP I will have to instruct the users to ensure the app is in the whitelist - I was hoping to get away from doing this as many users may not know how to do this.

How do I monitor the logs after I have disconnected the phones from B4A bridge?
 
Upvote 0
Top