I have an android widget that fetches data from a server every 10 minutes and display's it on the screen.
I'd like to add a "Refresh" button to that widget.
When the user clicks that button I'd like to run the method that fetches the information from the server.
Adding an event handler to a button in an application is very easy, however I couldn't find an example for a widget.
I'd like to get some help with adding a function to a button click in a widget.
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
I found out how to do that.
Add an action to the
AndroidManifest.xml
file in the ><receiver><intent-filter>
tag:In the provider add a constant that matches the action name:
In the
onUpdate()
method add a pending intent that matches the action:Finally, in the onRecieve() method, check the action name:
Here is another answer with the following benefits:
onReceive
in effect callsonUpdate
(so you reduce code duplication).onUpdate(Context context)
is generalised so that it can be dropped into any AppWidgetProvider subclass.The code:
The widget looks like this
This builds on the
getPendingSelfIntent
work of @Kels, @SharonHaimPour and @Erti-ChrisEelmaa.It also builds on Android-er > 2010-10-19 > Update Widget in onReceive() method (not me) where it is demonstrated how to call onUpdate from onReceive, on an App Widget instance basis. I make that code general and wrap it in
callOnUpdate
.Also prefer URL :
How to correctly handle click events on Widget
If you solved it in a different way, please provide this as an answer
Here is one example more that should help: