问题
So I've created a widget, and I'm tried my best to simulate a toggle button. Sense Widget doesn't support a toggle button I'm using a imagebutton and plan on changing the image depending on it's state.
Only problem is it's state isn't being saved and I'm not sure why.
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();
}
}
No matter how many times I press the button I always get the toast "beastmode is now ON"
Any suggestions?
Thank you.
EDIT:
Ok so I added this code:
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);
and my log gives me this back:
BEFORE beastmode is: false
AFTER beastmode is: true
It gives me this EVERY time I press the button. So obviously it creates a new instance or something to that effect. Once it executes it must destroy all variable values or something, maybe someone with more knowledge knows the right words.
So does anyone know a work around then?
回答1:
So I don't exactly know why the above didn't work, I'm assuming because it creates a new instance. I've managed to make a work around though by using SharedPreferences to keep track of the toggle. Probably not the best way to do it but it works, so I'm happy.
For anyone else having this problem here ya go:
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);
}
}
来源:https://stackoverflow.com/questions/10966595/how-to-make-a-toggle-button-for-a-widget-android