2014-09-05 59 views

回答

0

幾乎AppWidgetProviderClass的所有方法得到Context,讓我們一起來看看:

onAppWidgetOptionsChanged (Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) 
onDeleted(Context context, int[] appWidgetIds) 
onDisabled(Context context) 
onEnabled(Context context) 
onReceive(Context context, Intent intent) 
onUpdate (Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) 

所以,只要使用此背景下,你是好去:

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); 
0

您需要從上下文中獲取SharedPreferences實例,該實例在widget提供程序的onUpdate()方法中提供。事情是這樣的:分別

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME,0); 
} 

可以存儲和共享偏好負載布爾與putBoolean(鍵,值)和getBoolean(鍵,值)。

相關問題