AppWidget中的ImageButton更新源

发布于 2024-12-07 05:27:27 字数 1942 浏览 1 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

开始看清了 2024-12-14 05:27:27

是的,您必须更新小部件。从表面上看,您只需直接调用 onUpdate() 并读取其中的按钮状态即可相应地更新您的 RemoteViews 。只需使用 RemoteViews.setImageViewResource() 即可设置与当前按钮状态对应的图像。

调用 onUpdate() 所需的只是参数,您可以通过 Context 获取参数:

getAppWidgetIds()ComponentName 作为参数。
您可以创建如下所示的一个:

ComponentName cn = new ComponentName(context, WifiWidget.class);

您还可以使用整个构造从任何给定点更新您的应用程序小部件,您不受 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 your RemoteViews accordingly. Just use RemoteViews.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 a Context:

getAppWidgetIds() takes a ComponentName as an argument.
You can create one like below:

ComponentName cn = new ComponentName(context, WifiWidget.class);

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 addtional AppWidgetManager.updateAppWidget() call after you constructed your RemoteViews to publish them (which shows your UI changes).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文