سؤال

I am busy following this android tutorial on widgets.

In particular the part where you set-up a ConfigurationActivty.

Here are their steps:

  1. First, get the App Widget ID from the Intent that launched the Activity
  2. Perform your App Widget configuration.
  3. When the configuration is complete, get an instance of the AppWidgetManager by calling AppWidgetManager.getInstance()
  4. Update the App Widget with a RemoteViews layout by calling updateAppWidget(int, RemoteViews)
  5. 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);
}
هل كانت مفيدة؟

المحلول

App Widget configuration Activity is an optional Activity that launches when the user adds your App Widget and allows him or her to modify App Widget settings at create-time. In the configuration Activity you can set up update frequency, text in TextView, background drawable of the widget and so on ... It means that you are setting up the widget before it is created and visible. You can save all the settings in SharedPreferences and if you need to update or recreate the widget (after reboot or configuration change) you can get saved settings from SharedPreferences either in onUpdate method of your AppWidgetProvider or in your service UpdateWidgetService.java. Since you use UpdateWidgetService for update your widgets you should use SharedPreferences in this service.

Here is an example:

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

If you need more details please ask.

نصائح أخرى

You don't need to access appwidget_info.xml since it's shared by all widgets and you've declared it's usage at the Manifest. It will be automatically picked up by WidgetProvider. Perform your App Widget configuration. stage can be used for picking up some setting from SharedPrefences, which can be used for creating view later.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top