如何为小部件android制作切换按钮

时间:2012-06-10 05:36:14

标签: java android button widget togglebutton

所以我创建了一个小部件,我尽力模拟一个切换按钮。 Sense Widget不支持切换按钮我正在使用图像按钮,并计划根据图像的状态更改图像。

唯一的问题是它的状态没有被保存,我不知道为什么。

package com.tdubstudios.beastmode;

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.widget.RemoteViews;
import android.widget.Toast;

public class WidgetClass extends AppWidgetProvider{

private static final String ACTION_WIDGET_RECEIVER = "ActionRecieverWidget";  
private RemoteViews views;  

public boolean beastmodeOn = false;

public void onUpdate(Context context, AppWidgetManager appWidgetManager,  
       int[] appWidgetIds) {  
   final int N = appWidgetIds.length;
   views = new RemoteViews("com.tdubstudios.beastmode", R.layout.widget_layout);

   // Perform this loop procedure for each App Widget that belongs to this  
   // provider  
   for (int i = 0; i < N; i++) {  
       int appWidgetId = appWidgetIds[i];  
       Intent intent = new Intent(context, WidgetClass.class);  
       intent.setAction(ACTION_WIDGET_RECEIVER);  
       PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);  

       views.setOnClickPendingIntent(R.id.widget_ib, pendingIntent);  

       // Tell the AppWidgetManager to perform an update on the current App  
      // Widget  
       appWidgetManager.updateAppWidget(appWidgetId, views);  
   }
}  

@Override  
public void onReceive(Context context, Intent intent) {  
     if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) { 
         if(beastmodeOn){
             beastmodeOn = false;
             Toast.makeText(context, "beastmode is now off", Toast.LENGTH_SHORT).show();
         }else{
             beastmodeOn = true;
             Toast.makeText(context, "beastmode is now ON", Toast.LENGTH_SHORT).show();
         }
     }  
     super.onReceive(context, intent);  
}  

@Override
public void onDeleted(Context context, int[] appWidgetIds) {
    super.onDeleted(context, appWidgetIds);
    Toast.makeText(context, "onDelete has been called in widgetClass", Toast.LENGTH_LONG).show();
}

}

无论我按下按钮多少次,我总是得到祝酒“beastmode现在开启”

有什么建议吗?

谢谢。

编辑:

好的,我添加了这段代码:

Log.i("widget","BEFORE beastmode is: "+beastmodeOn);
             beastmodeOn = true;
             Toast.makeText(context, "beastmode is now ON", Toast.LENGTH_SHORT).show();
             Log.i("widget","AFTER beastmode is: "+beastmodeOn);

我的日志给了我回复:

 BEFORE beastmode is: false
 AFTER beastmode is: true

每次按下按钮都会给我这个。所以很明显它会创建一个新的实例或者那种效果。一旦它执行它必须销毁所有变量值或某事,也许有更多知识的人知道正确的单词。

那么有人知道当时的工作吗?

1 个答案:

答案 0 :(得分:1)

所以我不知道上面为什么不能正常工作,我假设是因为它创建了一个新实例。我已经设法通过使用SharedPreferences来跟踪切换来解决问题。可能不是最好的方法,但它有效,所以我很高兴。

对于其他任何有此问题的人你去:

package com.tdubstudios.beastmode;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.widget.RemoteViews;
import android.widget.Toast;

public class WidgetClass extends AppWidgetProvider {

    private static final String ACTION_WIDGET_RECEIVER = "ActionRecieverWidget";
    private RemoteViews views;

    public boolean beastmodeOn;

    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        final int N = appWidgetIds.length;
        views = new RemoteViews("com.tdubstudios.beastmode",
                R.layout.widget_layout);

        // Perform this loop procedure for each App Widget that belongs to this
        // provider
        for (int i = 0; i < N; i++) {
            int appWidgetId = appWidgetIds[i];
            Intent intent = new Intent(context, WidgetClass.class);
            intent.setAction(ACTION_WIDGET_RECEIVER);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                    0, intent, 0);

            views.setOnClickPendingIntent(R.id.widget_ib, pendingIntent);

            SharedPreferences prefs = PreferenceManager
                    .getDefaultSharedPreferences(context);
            boolean value = prefs.getBoolean("beastmodeOn", false);

            if (value) {
                Editor editor = prefs.edit();
                editor.putBoolean("beastmodeOn", false);
                editor.commit();
            }

            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {

            SharedPreferences prefs = PreferenceManager
                    .getDefaultSharedPreferences(context);
            boolean value = prefs.getBoolean("beastmodeOn", false);
            Editor editor = prefs.edit();

            if (value) {
                beastmodeOn = true;
            } else {
                beastmodeOn = false;
            }

            if (beastmodeOn) {
                Toast.makeText(context, "beastmode is now off",
                        Toast.LENGTH_SHORT).show();

                editor.putBoolean("beastmodeOn", false);
                editor.commit();
            } else {
                Toast.makeText(context, "beastmode is now ON",
                        Toast.LENGTH_SHORT).show();

                editor.putBoolean("beastmodeOn", true);
                editor.commit();
            }
        }
        super.onReceive(context, intent);
    }

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(context);
        boolean value = prefs.getBoolean("beastmodeOn", false);

        if (value) {
            Editor editor = prefs.edit();
            editor.putBoolean("beastmodeOn", false);
            editor.commit();
        }

        super.onDeleted(context, appWidgetIds);
    }
}