【发布时间】:2014-04-24 10:10:46
【问题描述】:
我正忙着关注this android tutorial on widgets。
尤其是您设置ConfigurationActivty 的部分。
这是他们的步骤:
- 首先,从启动 Activity 的 Intent 中获取 App Widget ID
- 执行您的应用小部件配置。
- 配置完成后,通过调用
AppWidgetManager.getInstance()获取AppWidgetManager的实例 - 通过调用
updateAppWidget(int, RemoteViews)使用RemoteViews布局更新应用小部件 - 最后,创建返回
Intent,用Activity结果设置它,完成Activity
我需要 2 方面的帮助:据我观察,人们正在使用 SharedPrefs,但是我如何实际访问提供有关小部件信息的 XML,例如更新频率:
<?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>
{Edit:} 好的,到目前为止我已经实现了这个:
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);
}
【问题讨论】:
-
你试过我的解决方案了吗?
标签: android android-widget android-appwidget