按钮单击事件为Android窗口小部件 [英] Button click event for android widget
问题描述
我有一个Android窗口小部件,从服务器获取数据每10分钟和显示的在屏幕上。
我想补充一个刷新按钮,该窗口小部件。
当用户点击该按钮,我想运行获取来自服务器的信息的方法。
将事件处理程序添加到按钮的应用程序是很容易的,但是我无法找到一个小部件的一个例子。
我想获得一些帮助,增加了功能,按一下按钮的一个小部件。
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.
推荐答案
我发现了如何做到这一点。
添加一个动作中> 的AndroidManifest.xml
文件><接收器GT;<意向滤光器> 标签:
I found out how to do that.
Add an action to the AndroidManifest.xml
file in the > <receiver><intent-filter>
tag:
<action android:name="MY_PACKAGE_NAME.WIDGET_BUTTON" />
在软件中添加一个常数相匹配的操作名称:
In the provider add a constant that matches the action name:
public static String WIDGET_BUTTON = "MY_PACKAGE_NAME.WIDGET_BUTTON";
在的OnUpdate()
方法中添加一个悬而未决的意图相匹配的行动:
In the onUpdate()
method add a pending intent that matches the action:
Intent intent = new Intent(WIDGET_BUTTON);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.MY_BUTTON_ID, pendingIntent );
最后,在onRecieve()方法,检查动作名称:
Finally, in the onRecieve() method, check the action name:
if (WIDGET_BUTTON.equals(intent.getAction())) {
//your code here
}
这篇关于按钮单击事件为Android窗口小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!