Android Widget:为切换小部件定义一个简单的布尔变量

时间:2011-04-07 22:00:45

标签: android android-widget toggle

我正在尝试制作一个非常简单的切换开关,我想存储开关的状态以便在状态之间切换。我目前的解决方案是使用类布尔变量,但变量似乎没有改变。

下面的源代码。正如您所看到的,到目前为止它非常简单并且触摸事件正在工作(也就是说,每次都会启动Log语句)。当小部件首次放置在屏幕上时,它具有文本“Hello!”。一旦第一次按下,这将变为“Ontrue”,因此显然isEnabled被设置为true。但是,在后续的按下时,Log显示isEnabled为false并且它继续通过else语句而不是if执行。

任何帮助将不胜感激!

package com.mywidget.widget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;

public class MyWidget extends AppWidgetProvider {
    public static final String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
    private RemoteViews remoteViews = new RemoteViews("com.mywidget.widget", R.layout.widget1x1);
    private boolean isEnabled;

    /** Called when the activity is first created. */
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        final int N = appWidgetIds.length;

        for (int i = 0; i < N; i++) {
            int appWidgetId = appWidgetIds[i];

            Intent intent = new Intent(context, MyWidget.class);
            intent.setAction(ACTION_WIDGET_RECEIVER);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
                    intent, 0);

            remoteViews.setOnClickPendingIntent(R.id.BTNwidget, pendingIntent);

            appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
        }
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
            Log.i("MyWidget", "Screen pressed! Value is: " + isEnabled);
            if (isEnabled) {
                // set to off
                Log.i("MyWidget", "Setting to off");

                // Code to change state
                isEnabled = false;

                // Update the widget to reflect our changes
                remoteViews.setTextViewText(R.id.BTNwidget, "OFF" + isEnabled);
                ComponentName cn = new ComponentName(context, HapticWidget.class);
                AppWidgetManager.getInstance(context).updateAppWidget(cn, remoteViews);
            } else {
                // set to on
                Log.i("MyWidget", "Setting to on");

                // Code to change state
                isEnabled = true;

                // Update the widget to reflect our changes
                remoteViews.setTextViewText(R.id.BTNwidget, "ON" + isEnabled);
                ComponentName cn = new ComponentName(context, HapticWidget.class);
                AppWidgetManager.getInstance(context).updateAppWidget(cn, remoteViews);
            }
        }
        super.onReceive(context, intent);
    }
}

1 个答案:

答案 0 :(得分:3)

AppWidgetProviderBroadcastReceiver,因此它的生命持续时间与onReceive()方法一样长。您的布尔值会在每次触摸时不断重新初始化为默认值。

您可以尝试使isEnabled静态,但您永远不知道运行接收器的进程(和VM)何时会被杀死(实际上我甚至不确定它是否会起作用)。所以你最好的机会是使用一些存储空间,我认为SharedPreferences会很好。