Android Question Help with widgets pls!

eSolution

Member
Licensed User
Longtime User
Hi, I have made an app with many content pages (rss, fb cntent, tweeter content and so on) and I decided to ad an home screen widget. I have read the tutorials and I made a nice looking widget that can change the background image, display a clock and a counter to a specific date. I have linked my widget to a specific settings page in my app but now the widget stop working and give me errors when it updates its state (once a minute - I have a clock in there)... I want to implement weather on that widget so I need to be able to have settings adjusted while the widget is running (location, temperature and so on) ... can someone post a small tutorial/template with a widget with settings page (that can modify things on the widget on the fly) ?

Thank you!
 

Jmu5667

Well-Known Member
Licensed User
Longtime User
Don't know if you can do that, however if you fire and intent from the widget that is received by your app you can that load the settings pages etc. If you need to send the changes back to the widget then fire an intent with the values required as extras in the intent. This is what I do and it always works regardless.
 
Last edited:
Upvote 0

eSolution

Member
Licensed User
Longtime User
Don't know if you can do that, however if you fire and intent from the widget that is received by your app you can that load the settings pages etc. If you need to send the changes back to the widget then fire an intent with the values required as extras in the intent. This is what I do and it always works regardless.
Hi :)

I'm kind of new to android programming (I do code in VB.NET but java and android is new to me) so I'm still learning about intents and I still have not found a comprehensive tutorial or explanation about "intents" as a concept ... Do you have some small code snippets on how to do what you suggest?
Thank you!
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
Ok, you need to get the broadcast receiver library. The next thing to do is define the different type of communications that you want.

Widget to App code:

Lets say the user can click an image on your widget. The image may represent a settings button.
The following code in the the service module of the widget

B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

   Dim Broadcast As BroadCastReceiver
   Dim rv As RemoteViews
 

   ' // widget intent broadcasts, intents are strings :)
   Dim ACTION_SHOWCLOCK As String = "yourapp.action.SHOWCLOCK"
   Dim ACTION_SHOWSETTINGS As String = "widget.action.SHOWSETTINGS"

end sub

Sub Service_Create
  
   rv = ConfigureHomeWidget("widgetlayout","rv",0,"Widget Title",True)
   ' // initialise the lib
   Broadcast.Initialize("Broadcast")
   ' // add the intent  you want to listen for
   Broadcast.addAction(ACTION_SHOWCLOCK )
   ' // set the priority
   Broadcast.SetPriority(999)
   ' // register the intent with android
   Broadcast.registerReceiver(ACTION_SHOWCLOCK )


End Sub

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

End Sub

Sub Service_Destroy

   Broadcast.unregisterReceiver

End Sub

Sub rv_RequestUpdate
   rv.UpdateWidget
End Sub

Sub rv_disabled
   StopService("")
End Sub

' // this is the client event of your image view control, the control is called 'ivSettings'
Sub ivSettings_Click
   
    ' // transmit "widget.action.SHOWSETTINGS"
    Broadcast.sendBroadcast(ACTION_SHOWSETTINGS)

End Sub

' // this will receive
Sub Broadcast_OnReceive (ACTION As String, i As Object)

   Dim In As Intent
  
   ' // get the intent data locally
   In = i
  
   Log("WIDGET " & In.ACTION & ", " & In.ExtrasToString )
    
 
   If ACTION = ACTION_SHOWCLOCK  Then
     ' // just checking for a key and it's value, you app will have set this
     If In.GetExtra("show_clock") = 1 Then
        'do show clock code
     End If
     If In.GetExtra("show_clock") = 0 Then
        'do hide clock code
     End If
   End If
  
   rv.UpdateWidget

End Sub

In you main app you can use the above code to add a broadcast receiver and listen for the intents that the widget can broadcast, namely ACTION_SHOWSETTINGS, so your main app will receive the intent because you registered to listen for.

I promise you that this is not hard, just new, you need to spend some time and play with it.

I hope the above helps you.

Regards

John.
 
Upvote 0
Top