我想允许添加一个小部件,我尝试放置一个布尔标志,我有:
在 WidgetConfig 内部:
public final static String ADDED_KEY = "IsAdded";
public static boolean isAdded = false;
onCreate(在 WidgetConfig 内)
isAdded = prefs.getBoolean(ADDED_KEY, false);
if (isAdded){
Toast.makeText(this.getApplicationContext(), "Only one Widget", Toast.LENGTH_SHORT).show();
finish();
}
[...]
editor.putBoolean(ADDED_KEY, true); //disable adding here
editor.commit();
WidgetProvider 内部扩展了 AppWidgetProvider
public void onDisabled(Context context) {
super.onDisabled(context);
[...]
//cancel alarm
configEditor.putBoolean(WidgetConfig.ADDED_KEY, false); //enable adding here
configEditor.commit();
}
public void onDeleted(Context context, int[] appWidgetIds){
super.onDeleted(context, appWidgetIds);
//I should not need this if there is only one widget
}
但它并不总是有效。我能怎么做?谢谢