Problem with updating the widget

schimanski

Well-Known Member
Licensed User
Longtime User
Why is it not possible to update the widget out of an activity module?
I tried the following code, but the widget doesn't dates up....

B4X:
'Service module of the widget (WidgetS)'
Sub Process_Globals
  Dim rv As RemoteViews
End Sub

Sub Service_Create
  rv = ConfigureHomeWidget("WidgetPortrait", "rv", 0, "Foxfinder",True)
End Sub

Sub Service_Start (StartingIntent As Intent)
  If rv.HandleWidgetEvents(StartingIntent) Then Return
End Sub

Sub rv_RequestUpdate
  Widget_check
End Sub

Sub Widget_check
  If Main.Passwortzugang=True Then 
    rv.SetTextColor("Label4",Colors.Green)
    rv.SetText("Label4","Aktiv")
  Else
    rv.SetTextColor("Label4",Colors.White)
    rv.SetText("Label4","Nicht aktiv")
  End If
   
  rv.UpdateWidget
End Sub

'Activity-Module'
B4X:
  .....
  CallSub(WidgetS,"Widget_check")
  ...

Thanks for help....
 

schimanski

Well-Known Member
Licensed User
Longtime User
Why souldn't the service work? When I set the widget to the screen, the Log calls 'Service (widgets) Start. And after that, the service of the widget still runs. There is no pause in the log...
 
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
The Log("Check WidgetS:" & IsPaused(WidgetS)) tells me 'false'. So the service is just running, or?
 
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
Thanks for help, but it is a very big project. When the code I have posted have to work, I will try the widget in a small simple app. I tested it and call back....
 
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
I have posted the small widget-code. It doesn't work...
 

Attachments

  • HomeWidgets.zip
    10.7 KB · Views: 302
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I believe that my previous assumption was correct. The process eventually gets killed by Android. Then IsPaused will return True and CallSub will not do anything.
The solution is to start the service yourself:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("L1.bal")
   StartService(WidgetService)
End Sub
You a task manager to test it. Kill the process and you will see that it will now work.

However you must also persist the current state.
Whenever something changes you need to store it in a file. Then in Service_Create you should look for the file and read the state from the file.

It should be pretty easy to do it with File.ReadMap / WriteMap.
 
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
Hello!

I have tried your example, but it doesn't work. Than I have installed it on my Galaxy Tab P 1000, and it runs very fine :BangHead:..

It don't works on my Motorola Defy. I have also tried to start the WidgetService each time, before I call

'CallSub(WidgetService,"Widget_check")',

but the same problem... What is the different between Defy and Galaxy Tab? Both devices are under Froyo...
 
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
O.k, I understand.
But I have tried something else and it seems, that it is not possible to update a widget on a motorola Defy. Is there perhaps another way to handle the update?

Thanks for your efforts..
 
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
I have changed the code in the Service_start to the following and it runs on the Motorola Defy. The problem is, that the system is slowing down, when the widget is installed on the homescreen. I don't know why the following code runs on the defy. Is there someone who can see the problem?

Thanks...

B4X:
Sub Service_Start (StartingIntent As Intent)

  If StartingIntent.Action = "android.appwidget.action.APPWIDGET_DELETED"  Then Return
   
  If rv.HandleWidgetEvents(StartingIntent) = False Then
    rv_RequestUpdate
  End If

  If StartingIntent.Action <> "android.appwidget.action.APPWIDGET_DISABLED" Then
    StartServiceAt("", DateTime.Now+30, False)
  End If

End Sub
 

Attachments

  • WidgetTest.zip
    10.7 KB · Views: 290
Upvote 0

corwin42

Expert
Licensed User
Longtime User
B4X:
    StartServiceAt("", DateTime.Now+30, False)

With this your widget gets updated every 30ms which is 33 times a second. Quite often I think. :cool:
 
Upvote 0
Top