Android Question Chicken and egg problem with Home screen widget and B4XPages

Alessandro71

Well-Known Member
Licensed User
Longtime User
Based on the widget example https://www.b4x.com/android/forum/threads/android-home-screen-widgets-tutorial-part-i.10166/ I added a widget to and already existing B4XPages app.
The widget displays the bitmap taken with the Snapshot method from a B4XView.
The B4XView is available only when the app is running, while the widget service receives updates requests even when the app is not running.

Here is the code from the Widget service: what I see in the logs, is that the RequestUpdate gets called, but B4XPages is not available, thus skipping widget update

B4X:
Sub rv_RequestUpdate
    WriteLog("updating")

    If (B4XPages.IsInitialized) Then
        If (B4XPages.MainPage.IsInitialized) Then
            If (B4XPages.MainPage.DownloadNeeded) Then
                WriteLog("data download needed")
                Wait For (B4XPages.MainPage.DownloadData) Complete (success As Boolean)
                WriteLog("data downloaded")
            Else
                WriteLog("data still fresh")
            End If
            B4XPages.MainPage.UpdateChartsForWidget      'updates bitmap in Starter service

            rv.SetImage("IV_widgettrend1", Starter.bitmapwidget)

            rv.UpdateWidget
        Else
            WriteLog("Mainpage not available")
        End If
    Else
        WriteLog("B4XPages not available")
    End If
End Sub
 

Alessandro71

Well-Known Member
Licensed User
Longtime User
This is expected. The process is started from the service and no UI has been created. It is up to you to handle this case.
I understand, so the correct question should be: can I start the B4XPages framework from a widget service?
I’m afraid of the answer…
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
can I start the B4XPages framework from a widget service?
More correct: can I start an activity from a service (starting Main will cause the pages to start)?
Answer: Starting from Android 10 it requires the special draw over apps permission. If you get this permission then you can call StartActivity(Main) to start the pages.
 
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
More correct: can I start an activity from a service (starting Main will cause the pages to start)?
Answer: Starting from Android 10 it requires the special draw over apps permission. If you get this permission then you can call StartActivity(Main) to start the pages.
This is way more that's what's really needed: I'll trim it down further.
Can I instantiate a custom view in a service and get its bitmap?
 
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
While exploring this problem, I came to this showstopper:

B4X:
Sub rv_RequestUpdate
    Dim xui As XUI
    Dim panel As B4XView = xui.CreatePanel("")
    Dim xC_chartwidget1 As xChart
    panel.SetLayoutAnimated(0, 0dip, 0dip, 294dip, 144dip)
    panel.LoadLayout("widgethelper")
        
    xC_chartwidget1.DrawEmptyChart

    rv.SetImage("IV_widgettrend1", xC_chartwidget1.Snapshot)
End Sub

where widgethelper.bal is a layout containing xC_chartwidget1

but this fails with an exception that seems related to the panel

(NullPointerException) java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.ref.WeakReference.get()' on a null object reference
 
Upvote 0
Top