如何保存小部件的配置

问题描述:

我正忙于关注this android tutorial on widgets如何保存小部件的配置

特别是您设置ConfigurationActivty的部分。

这是他们的步骤:

  1. 首先,从推出的活动
  2. 执行你的App窗口配置的意图得到应用的Widget ID。
  3. 当配置完成后,通过调用AppWidgetManager.getInstance()
  4. 更新应用的Widget与RemoteViews布局通过调用updateAppWidget(int, RemoteViews)
  5. 最后得到AppWidgetManager的一个实例,创建返回Intent,用Activity的结果集,并完成Activity

我需要帮助以2:从我瞪大眼睛人使用SharedPrefs,但实际上,我怎么访问我的XML这给信息有关的部件,如更新频率uency:

<?xml version="1.0" encoding="utf-8"?> 
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 
    android:initialLayout="@layout/widget_layout" 
    android:minHeight="200dp" 
    android:minWidth="144dp" 
    android:widgetCategory="home_screen" 
    android:configure="widget.AppWidgetConfigureActivity" 
    android:updatePeriodMillis="1800000" > 

</appwidget-provider> 

{编辑:}好吧,我迄今已实现了这个:

private void SaveWidgetConfiguration() { 

    int deviceTypeId = 0; 
    int deviceId = 0; 
    String hashedPasscode = ""; 
    int updateFreq = 30000; 

    SharedPreferences prefs = AppWidgetConfigureActivity.this.getSharedPreferences("prefs", 0); 
    SharedPreferences.Editor edit = prefs.edit(); 
    edit.putInt("Widget_DeviceTypeId:" + appWidgetId, deviceTypeId); 
    edit.putLong("Widget_DeviceId:" + appWidgetId, deviceId); 
    edit.putString("Widget_Passcode:" + appWidgetId, hashedPasscode); 
    edit.putInt("Widget_UpdateFreq:" + appWidgetId, updateFreq); 
    edit.commit(); 
} 

但现在在哪里以及如何获得这些优惠?

我正在使用服务来更新我的小部件。我在MyWidgetProvider中获取它们吗?

我目前MyWidgetProvider:

public class MyWidgetProvider extends AppWidgetProvider { 

private static final String LOG = "de.vogella.android.widget.example"; 

@Override 
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 

    Log.w(LOG, "onUpdate method called"); 
    // Get all ids 
    ComponentName thisWidget = new ComponentName(context, MyWidgetProvider.class); 
    int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget); 

    // Get Preferences: 

    // Build the intent to call the service 
    Intent intent = new Intent(context.getApplicationContext(), UpdateWidgetService.class); 
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds); 

    // Update the widgets via the service 
    context.startService(intent); 
} 
+0

你试过我的解决方案吗? – janzoner

应用小部件配置活动是一个可选的活动,当用户添加您的应用小部件并允许他在创建时修改应用小部件设置时启动。在配置活动中,您可以设置更新频率,TextView中的文本,小部件的背景可绘制等等。这意味着您在创建小部件并可见之前设置小部件。您可以将所有设置保存在SharedPreferences中,并且如果您需要更新或重新创建小组件(重新启动或配置更改后),则可以从SharedPreferences获取保存的设置,您的onUpdate方法为AppWidgetProvider或在您的服务UpdateWidgetService.java中。由于您使用UpdateWidgetService来更新您的小部件,因此您应该在此服务中使用SharedPreferences

下面是一个例子:

Context context = getApplicationContext(); 

SharedPreferences prefs = context.getSharedPreferences("prefs", 0); 
int deviceTypeId = prefs.getInt("Widget_DeviceTypeId:" + appWidgetId, defValue); // defValue is used if the preference doesn't exist 
// get all other preferences/settings and use them to update the widget 

如果您需要了解更多详情请询问。

你并不需要,因为它是由所有小工具共享,你已经宣布它在Manifest使用访问appwidget_info.xml。它将自动被WidgetProvider接收。 Perform your App Widget configuration.阶段可用于从SharedPrefences获取一些设置,可用于稍后创建视图。

+0

请参阅我的编辑。然后,我看看我的WidgetProviderBoarcast的偏好,然后根据参数生成适当的布局? – Zapnologica