I am busy following this android tutorial on widgets.
In particular the part where you set-up a ConfigurationActivty
.
Here are their steps:
- First, get the App Widget ID from the Intent that launched the Activity
- Perform your App Widget configuration.
- When the configuration is complete, get an instance of the
AppWidgetManager
by calling AppWidgetManager.getInstance()
- Update the App Widget with a
RemoteViews
layout by calling updateAppWidget(int, RemoteViews)
- Finally, create the return
Intent
, set it with the Activity
result, and finish the Activity
I need help with 2: from what I have goggled people are using the SharedPrefs
, But how do I actually access my XML which gives info about the widget, such as update frequency:
<?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:} Ok i have implemented this so far:
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();
}
But now where and how do I get these preferences?
I am using a service to update my widget. Do I get them in MyWidgetProvider?
My Current 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);
}