AppWidget中的ImageButton更新源
我正在尝试在 RemoteViews 方面复制 ToggleButton 的功能,这样我就可以在我的应用程序小部件中使用它。我有一个 ImageButton,并且它可以检测点击,但我在更改点击时显示的图像时遇到问题。我的印象是,在单击小部件以显示更改后,我需要强制更新小部件。知道最好的方法是什么吗?
public class WifiWidget extends AppWidgetProvider {
boolean toggleState = false;
public static String ACTION_WIDGET_RECEIVER = "com.nickavv.cleanwidgets.WIFICLICK";
Context myContext;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int appWidgetIds[]) {
final int N = appWidgetIds.length;
myContext = context;
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
RemoteViews views = new RemoteViews("com.nickavv.cleanwidgets", R.layout.wifi_toggle_layout);
Intent intent = new Intent(ACTION_WIDGET_RECEIVER);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.wifiToggleButton, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
switchToggle();
}
}
void switchToggle() {
RemoteViews views = new RemoteViews("com.nickavv.cleanwidgets", R.layout.wifi_toggle_layout);
Log.d("toggleState",""+toggleState);
if(toggleState == false) {
views.setImageViewResource(R.id.wifiToggleButton, R.drawable.wifi_toggle_on);
} else {
views.setImageViewResource(R.id.wifiToggleButton, R.drawable.wifi_toggle_off);
}
toggleState = !toggleState;
Log.d("toggleState",""+toggleState);
}
编辑:将其添加到 switchToggle 的底部:
AppWidgetManager myAWM = AppWidgetManager.getInstance(myContext);
ComponentName cn = new ComponentName(myContext, WifiWidget.class);
onUpdate(myContext, myAWM, myAWM.getAppWidgetIds(cn));
I'm trying to replicate the functionality of a ToggleButton in terms of RemoteViews, so I can have on in my App Widget. I have an ImageButton, and I've got it detecting clicks, but I'm having trouble changing the image that it displays when clicked. I'm under the impression I need to force the widget to update after it's been clicked to show the changes. Any idea what's the best way to go about it?
public class WifiWidget extends AppWidgetProvider {
boolean toggleState = false;
public static String ACTION_WIDGET_RECEIVER = "com.nickavv.cleanwidgets.WIFICLICK";
Context myContext;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int appWidgetIds[]) {
final int N = appWidgetIds.length;
myContext = context;
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
RemoteViews views = new RemoteViews("com.nickavv.cleanwidgets", R.layout.wifi_toggle_layout);
Intent intent = new Intent(ACTION_WIDGET_RECEIVER);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.wifiToggleButton, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
switchToggle();
}
}
void switchToggle() {
RemoteViews views = new RemoteViews("com.nickavv.cleanwidgets", R.layout.wifi_toggle_layout);
Log.d("toggleState",""+toggleState);
if(toggleState == false) {
views.setImageViewResource(R.id.wifiToggleButton, R.drawable.wifi_toggle_on);
} else {
views.setImageViewResource(R.id.wifiToggleButton, R.drawable.wifi_toggle_off);
}
toggleState = !toggleState;
Log.d("toggleState",""+toggleState);
}
EDIT: Added this to the bottom of switchToggle:
AppWidgetManager myAWM = AppWidgetManager.getInstance(myContext);
ComponentName cn = new ComponentName(myContext, WifiWidget.class);
onUpdate(myContext, myAWM, myAWM.getAppWidgetIds(cn));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您必须更新小部件。从表面上看,您只需直接调用
onUpdate()
并读取其中的按钮状态即可相应地更新您的RemoteViews
。只需使用 RemoteViews.setImageViewResource() 即可设置与当前按钮状态对应的图像。调用
onUpdate()
所需的只是参数,您可以通过Context
获取参数:switchToggle()
并转到onUpdate()
AppWidgetManager.getInstance(context)
AppWidgetManager.getAppWidgetIds()
getAppWidgetIds()
将ComponentName
作为参数。您可以创建如下所示的一个:
您还可以使用整个构造从任何给定点更新您的应用程序小部件,您不受
onUpdate()
的约束。只需在构建RemoteViews
后使用额外的AppWidgetManager.updateAppWidget()
调用即可发布它们(这会显示您的 UI 更改)。Yes you have to update the widget. From the looks of this you can just call
onUpdate()
directly and read the button state in there to update yourRemoteViews
accordingly. Just useRemoteViews.setImageViewResource()
to set the image that corresponds to the current button state.All you need to call
onUpdate()
is the arguments, which you can get via aContext
:switchToggle()
and on toonUpdate()
AppWidgetManager.getInstance(context)
AppWidgetManager.getAppWidgetIds()
getAppWidgetIds()
takes aComponentName
as an argument.You can create one like below:
You can also use this whole construct to update your app widgets from any given point, you are not bound to
onUpdate()
. Just use an addtionalAppWidgetManager.updateAppWidget()
call after you constructed yourRemoteViews
to publish them (which shows your UI changes).