Android Question Home widget not updating on Android 9+

DavideV

Active Member
Licensed User
Longtime User
Hello developers,
time ago I wrote a small app for a Battery monitor home widget, following some tutorials on this forum and including last updates for Android 8+ here:
Automatic Foreground and it worked fine also with Android 9.

Yesterday my own phone (Xiaomi) received the Android 10 updates and, since then, the widget does not update anymore.
I think it is something related to power management.
Did someone of you managed to solve this problem?

Also I think it is not possible to change power options programmatically, probably the OS doesn't allow to do it.
Is there a way to open the battery options for the app, to ask the user to change the settings manually?

Thanks in advance!
Davide
 

DavideV

Active Member
Licensed User
Longtime User
Hi Erel,
the app runs in release mode.
Here is the service start code:

Service_Start:
Sub Service_Start (StartingIntent As Intent)
    If StartingIntent.IsInitialized=True Then
        'started by an intent
        #if dev
        LogFile("SBW> service started by an intent: " & StartingIntent.ExtrasToString,False)
        #End If
        
        rv.HandleWidgetEvents(StartingIntent)
    End If
    
    Dim Action As String=StartingIntent.Action
    #if dev
    Log("SBW> Intent starting action: " & Action)
    #End If

    rv_RequestUpdate
    
    'Intent starting action: android.appwidget.action.APPWIDGET_ENABLED
    'Intent starting action: android.appwidget.action.APPWIDGET_UPDATE
    Select Case Action
        Case "android.appwidget.action.APPWIDGET_ENABLED"
            'rv.HandleWidgetEvents(StartingIntent)
            
        Case "android.appwidget.action.APPWIDGET_UPDATE"
            'rv.HandleWidgetEvents(StartingIntent)
            
        Case Else
            'started by system?
            #if dev
                Log("SBW> service started by system after a destroy")
            #End If
        
            
    End Select
    
    'fix For Android 8, requires B4A V8
    StartServiceAt(Me,DateTime.Now+60*1000,False)    'restart the service every 60 sec , not when in stanby
    Sleep(0)
    Service.StopAutomaticForeground
    
End Sub

Thanks
 
Upvote 0

DavideV

Active Member
Licensed User
Longtime User
I found that it seems working if I set the app power managent (under device settings) to No restriction , instead of energy saving mode, for this specific app...
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
I own a Xiaomi device but it looks that my widget example continue to run normally.
The battery property for the relative app is set to Save Energy (by default, never touched it).
 
Upvote 0

DavideV

Active Member
Licensed User
Longtime User
I own a Xiaomi device but it looks that my widget example continue to run normally.
The battery property for the relative app is set to Save Energy (by default, never touched it).
It worked fine for me too with Android 9, until new android 10 update, installed yesterday
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is a mistake: StartServiceAt(Me,DateTime.Now+60*1000,False) 'restart the service every 60 sec , not when in stanby

It will not do anything positive.

You don't need to check whether StartingIntent is initialized and your code will crash anyway on the next line.

Have you made the service a sticky service (don't) ?
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
I am running Android 10.
But it could depend even on the device model.
If you want to try my widget you can get it here
Widget
The widget is useless, it just show some labels and the yellow one write the current hour in letters.
Nothing more.
But you can check if it update regularly every 30 minutes.
Of course You will have proof of it only 1 time per hour. ?
Just to have a second test.
 
Upvote 0

DavideV

Active Member
Licensed User
Longtime User
At the beginning the service was Sticky and using the battery intent from the device itself. But after a certain version of the OS the battery intend did not work properly and I switched the service to check battery every 60 secs
 
Upvote 0

DavideV

Active Member
Licensed User
Longtime User

I found that page this morning, the code is very close to mine but with extras... I will give it a try too.
Anyway it looks the problem is related to the power management per app...
For now it works as expected using 'No Restriction'.

It will be nice to let the user makes changes to power management opening the setting from the app...
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…