Android Question [Solved] How to update widget (receiver) from the B4XPages app and open app from widget?

AnandGupta

Expert
Licensed User
Longtime User
Please see post #6 for solution project source code for update widget from the app and open the app from the widget.


Taking widget (receiver) example,

I added a button to the main page.
I need to update the widget (current time) when I press the button of the app.

Since it is a B4xPages app, I can not
B4X:
    Activity.Finish
which I did in Activity (old) app.

So, what code should I write here ?
B4X:
Private Sub Button1_Click
    xui.MsgboxAsync("Update Widget. " & DateTime.Time(DateTime.Now), "")
    ' want to update the widget now.
    ' what code  to write here ?
    ' WidgetReceiver. ??
End Sub

I did search the Forum but could not find any example/solution for it.
Attached is the project with the changes.
 

Attachments

  • HomeWidgets.zip
    25 KB · Views: 100
Last edited:
Solution
So, what code should I write here ?
Try
B4X:
Private Sub Button1_Click
    xui.MsgboxAsync("Update Widget. " & DateTime.Time(DateTime.Now), "")
    ' want to update the widget now.
    ' what code  to write here ?
    ' WidgetReceiver. ??
    CallSubDelayed(WidgetReceiver, "ImageView1_Click")
End Sub

teddybear

Well-Known Member
Licensed User
Private Sub Button1_Click
xui.MsgboxAsync("Update Widget. " & DateTime.Time(DateTime.Now), "")
' want to update the widget now.
' what code to write here ?
' WidgetReceiver. ??
End Sub

[/code]

I did search the Forum but could not find any example/solution for it.
Attached is the project with the changes.
Send broadcast intent to WidgetReceiver.
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
So, what code should I write here ?
Try
B4X:
Private Sub Button1_Click
    xui.MsgboxAsync("Update Widget. " & DateTime.Time(DateTime.Now), "")
    ' want to update the widget now.
    ' what code  to write here ?
    ' WidgetReceiver. ??
    CallSubDelayed(WidgetReceiver, "ImageView1_Click")
End Sub
 
Upvote 0
Solution

AnandGupta

Expert
Licensed User
Longtime User
Try
B4X:
Private Sub Button1_Click
    xui.MsgboxAsync("Update Widget. " & DateTime.Time(DateTime.Now), "")
    ' want to update the widget now.
    ' what code  to write here ?
    ' WidgetReceiver. ??
    CallSubDelayed(WidgetReceiver, "ImageView1_Click")
End Sub
Thanks a Lot ?
It worked ?
 
Upvote 0

AnandGupta

Expert
Licensed User
Longtime User
I wanted to update widget from the app and open the app from the widget.
Attached is the project which does both.

Hope it help members who may look for similar solution.
 

Attachments

  • HomeWidgets.zip
    25.2 KB · Views: 127
Upvote 0

pixet

Member
Licensed User
Longtime User
Great work @Sagenut, @AnandGupta.
How can I periodically update the contents of the Widget,
I was thinking of an AppWidgetProvider class or similar where I collect my new contents and send them to the Widget.
 
Last edited:
Upvote 0

pixet

Member
Licensed User
Longtime User
The Timer works needs improvement but in general it works until you close the app. If you close the app, updating of the widget contents stops. To update the widget contents again, you must reopen the app from the "OPEN" button on the widget and everything will work again.
I'd like it to continue working even without the app open in the background. I should probably activate a Service or something.

the code of my Receiver: WidgetReceiver:
Sub Process_Globals
    Private rv As RemoteViews
    Private imageFiles() As String = Array As String("player_button_next.png", "player_button_pause.png", _
        "player_button_play.png", "player_button_stop.png")
    Private currentImage As Int
   
    Private hgg As PhoneEvents
    Private BatLevel As Int
    Private Timer1 As Timer
End Sub

'Called when an intent is received.
'Do not assume that anything else, including the starter service, has run before this method.
Private Sub Receiver_Receive (FirstTime As Boolean, StartingIntent As Intent)
    Timer1.Initialize("Timer1", 1000)
    If FirstTime Then
        Log("Enter the first time and activate the Receiver")
        rv = ConfigureHomeWidget("L1", "rv", 60, "Hello Widget")
        hgg.Initialize("hgg")
        Sleep(200)
       
        Button1_Click

    End If
    Timer1.Enabled = True
    rv.HandleWidgetEvents(StartingIntent)
    Sleep(500)
    rv.UpdateWidget
   
    Timer1.Enabled = True
   
End Sub

Private Sub rv_RequestUpdate
    Log("rv_RequestUpdate: Message received by Receiver")
    ImageView1_Click
    SetTime
    rv.UpdateWidget
End Sub

Private Sub SetTime
    Log("SetTime: Update time")
    rv.SetText("Label1", DateTime.Time(DateTime.Now))
End Sub

Private Sub ImageView1_Click
    Log("ImageView1_Click: Update widget contents")
    currentImage = (currentImage + 1) Mod imageFiles.Length
    rv.SetImage("ImageView1", LoadBitmap(File.DirAssets, imageFiles(currentImage)))
    'rv.SetProgress("ProgressBar1", 100 * (currentImage + 1) / imageFiles.Length)
    rv.SetProgress("ProgressBar1", BatLevel)
    rv.SetText("Label2", BatLevel & "%")
    SetTime
    rv.UpdateWidget
End Sub

Private Sub Button1_Click
    Log("WidgetReceiver --> Button1_Click: Opens Activity Main")
    StartActivity(Main)
End Sub

Sub Timer1_Tick
    ' event Timer1
    Log("Timer1: event")
    Timer1.Enabled = False
    'ImageView1_Click
    rv.SetProgress("ProgressBar1", BatLevel)
    rv.SetText("Label2", BatLevel & "%")
    SetTime

    rv.UpdateWidget
    Timer1.Enabled = True
End Sub

Public Sub hgg_BatteryChanged (Level As Int, Scale As Int, Plugged As Boolean, Intent As Intent)
    ' Get Battery level
    BatLevel = Level
End Sub
 

Attachments

  • HomeWidgetsd04.zip
    18.3 KB · Views: 83
Upvote 0

AnandGupta

Expert
Licensed User
Longtime User
If you close the app, updating of the widget contents stops.
May be due to this,
Is StartActivity() the correct way to open app from widget for B4XPages project ? | B4X Programming Forum
you cannot start an activity while the app is in the background.

You can try putting timer in Startup or main, and see if it helps.
 
Upvote 0

pixet

Member
Licensed User
Longtime User
update 05

StartActivity(Main) seems to work as expected.

Added two ways to test exit/close app/activity
and it seems to work, choosing "ClosExit App" kills the app and removes it from the list of running apps but the widget remains active and all its functions work.

After installing the app to check that everything was correct, I restarted the smartphone and the widget started normally and remains active and updates the contents regularly.

I edited and added in B4XMainPage:

B4XMainPage.bas:
#Region Shared Files
#CustomBuildAction: folders ready, %WINDIR%\System32\Robocopy.exe,"..\..\Shared Files" "..\Files"
'Ctrl + click to sync files: ide://run?file=%WINDIR%\System32\Robocopy.exe&args=..\..\Shared+Files&args=..\Files&FilesSync=True
#End Region

'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=HomeWidgets.zip

Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Public bat_lvl As B4XView
    Public BatteryCharging As Boolean
    
    Private hg As PhoneEvents
    Private bntClose As Button
    Private btnFinish As B4XView
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    hg.Initialize("hg")   
    xui.MsgboxAsync("Go to the home screen and add the <Hello Widget>.", "")
End Sub

Public Sub Button1_Click
    Log("B4XMainPAge: Button1_Click")
    xui.MsgboxAsync("Update Widget. " & DateTime.Time(DateTime.Now), "")
    CallSubDelayed(WidgetReceiver, "ImageView1_Click")
End Sub

Sub bntClose_Click
    '' Close App
    Dim jo As JavaObject
    jo.InitializeContext
    jo.RunMethod("finishAndRemoveTask", Null)
End Sub

Sub btnFinish_Click
    ' finish activity B4XPages
    B4XPages.GetNativeParent(Me).Finish
End Sub

Public Sub hg_BatteryChanged (Level As Int, Scale As Int, Plugged As Boolean, Intent As Intent)
    bat_lvl.Text = Level
    ' & " %"    ' legge livello batteria dagli eventi del telefono
    BatteryCharging = Plugged
    Log("Level: " & Level & "%, Scale: " & Scale & ", Plugged: " & Plugged)
End Sub
 

Attachments

  • HomeWidgetsd05.zip
    18.9 KB · Views: 87
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
you cannot start an activity while the app is in the background.
Not exactly. Read Erels Answer again.
If you use the DrawOverOtherApps Permission it is possible.
 
Upvote 0
Top