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
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.
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.
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