[android-developers] Re: Working with Multiple Widget Instances/Layout

2011-03-21 Thread Jake Colman
 KV == Kostya Vasilyev kmans...@gmail.com writes:

   KV 21.03.2011 17:01, Jake Colman пишет:
1) How does one provide multiple options for widget sizes?  Do you just
add additional meta-data sections under the Receiver entry of the
manifest?

   KV Essentially by providing entirely separate widgets, each having
   KV its own size. They can share implementation (Java code,
   KV drawables, layouts), but they should be declared as separate
   KV widgets as seen by Android.

How does one declare them as separate widgets as seen by Android?
Again, we are talking about a single project (apk) with one manifest,
correct?  So how do you modify the manifest to indicate that is more
than one widget?

2) How does one set options that are unique to a given instance?  The app
currently uses SharedPreferences for all of its configuration.
But if both instances are running in the same context, how can
each widget know how it is configured as distinct from another
instance?

   KV Widget ids are persistent until a widget is removed. Store values
   KV in shared preferences (or some other persistence mechanism) keyed
   KV by widget id.

H.  Using that standard SharedPreferences, I did not see that it
could be keyed.  Did I miss that?  Each piece of configuration data is,
of course, keyed by the name of that piece of data.  But how would I
distinguish between the configuration data of two different widgets if
they are all stored in the same SharedPreferences?

3) The app uses a service that is started by an alarm in order to do the
updating.  When the alarm is triggered, the service calculates the
time remaining until the event and then it updates the remote
view.  Do both widgets share the same service?  I'd that is the
case since it all one app with just multiple widget instances.  If
that's the case, how can the service know which widget it is
updating?

   KV Use

   KV AppWidgetManager.updateAppWidget(int widgetId, RemoteViews views)

   KV to update only one particular widget.

   KV You can get the ids when needed outside onUpdate like this:

   KV AppWidgetManager manager = AppWidgetManager.getInstance(context);
   KV ComponentName widgetClassName = new ComponentName(context, your
   WidgetProviderClass );
   KV int[] widgetIds = manager.getAppWidgetIds(widgetClassName);

   KV Be aware that in some cases, there may be widget ids that refer to
   KV non-existent widgets.

   KV I documented my workaround for this issue here:

   KV 
http://kmansoft.wordpress.com/2010/06/22/per-widget-options-stale-widgets/

And then the service would have to ask that instance which type of
data (sunrise or sunset) it should be updating.  But how to ask that
question?

-- 
Jake Colman -- Android Tinkerer

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


Re: [android-developers] Re: Working with Multiple Widget Instances/Layout

2011-03-21 Thread Kostya Vasilyev

See below:

21.03.2011 21:37, Jake Colman пишет:

KV == Kostya Vasilyevkmans...@gmail.com  writes:

KV  21.03.2011 17:01, Jake Colman пишет:
  1) How does one provide multiple options for widget sizes?  Do you just
  add additional meta-data sections under the Receiver entry of the
  manifest?

KV  Essentially by providing entirely separate widgets, each having
KV  its own size. They can share implementation (Java code,
KV  drawables, layouts), but they should be declared as separate
KV  widgets as seen by Android.

How does one declare them as separate widgets as seen by Android?
Again, we are talking about a single project (apk) with one manifest,
correct?  So how do you modify the manifest to indicate that is more
than one widget?


Same as declaring one widget, but repeating as necessary:

receiver android:name=.MyWidget_3x1 
android:label=@string/widget_name_3x1

intent-filter ... /
meta-data
android:name=android.appwidget.provider
android:resource=@xml/widget_info_3x1 /
/receiver

and then

receiver android:name=.MyWidget_2x1 
android:label=@string/widget_name_2x1

intent-filter ... /
meta-data
android:name=android.appwidget.provider
android:resource=@xml/widget_info_2x1 /
/receiver

The key part is that this lets you reference two distinct 
xml/widget_info_3x1 and xml/widget_info_2x1.


This is important because those specify android:minWidth and 
android:minHeight, which can't be changed at runtime.




  2) How does one set options that are unique to a given instance?  The 
app
  currently uses SharedPreferences for all of its configuration.
  But if both instances are running in the same context, how can
  each widget know how it is configured as distinct from another
  instance?

KV  Widget ids are persistent until a widget is removed. Store values
KV  in shared preferences (or some other persistence mechanism) keyed
KV  by widget id.

H.  Using that standard SharedPreferences, I did not see that it
could be keyed.  Did I miss that?  Each piece of configuration data is,
of course, keyed by the name of that piece of data.  But how would I
distinguish between the configuration data of two different widgets if
they are all stored in the same SharedPreferences?


String key = Blah + String.valueOf(widgetId);
prefs.getInt(key, 0);

etc, etc etc.



  3) The app uses a service that is started by an alarm in order to do the
  updating.  When the alarm is triggered, the service calculates the
  time remaining until the event and then it updates the remote
  view.  Do both widgets share the same service?  I'd that is the
  case since it all one app with just multiple widget instances.  If
  that's the case, how can the service know which widget it is
  updating?

KV  Use

KV  AppWidgetManager.updateAppWidget(int widgetId, RemoteViews views)

KV  to update only one particular widget.

KV  You can get the ids when needed outside onUpdate like this:

KV  AppWidgetManager manager = AppWidgetManager.getInstance(context);
KV  ComponentName widgetClassName = new ComponentName(context,your
WidgetProviderClass  );
KV  int[] widgetIds = manager.getAppWidgetIds(widgetClassName);

KV  Be aware that in some cases, there may be widget ids that refer to
KV  non-existent widgets.

KV  I documented my workaround for this issue here:

KV  
http://kmansoft.wordpress.com/2010/06/22/per-widget-options-stale-widgets/

And then the service would have to ask that instance which type of
data (sunrise or sunset) it should be updating.  But how to ask that
question?


You can let the user specify this in the widget's configuration 
activity, then store that setting in SharedPrefs, and retrieve as needed.


--
Kostya Vasilyev -- http://kmansoft.wordpress.com

--
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


[android-developers] Re: Working with Multiple Widget Instances/Layout

2011-03-21 Thread Jake Colman
 KV == Kostya Vasilyev kmans...@gmail.com writes:

   KV See below:
   KV 21.03.2011 21:37, Jake Colman пишет:
KV == Kostya Vasilyevkmans...@gmail.com  writes:

   KV 21.03.2011 17:01, Jake Colman пишет:
  1) How does one provide multiple options for widget sizes?  Do
 you just add additional meta-data sections under the
 Receiver entry of the manifest?

   KV Essentially by providing entirely separate widgets, each having
   KV its own size. They can share implementation (Java code,
   KV drawables, layouts), but they should be declared as separate
   KV widgets as seen by Android.

How does one declare them as separate widgets as seen by
Android?  Again, we are talking about a single project (apk) with
one manifest, correct?  So how do you modify the manifest to
indicate that is more than one widget?

   KV Same as declaring one widget, but repeating as necessary:

   KV receiver android:name=.MyWidget_3x1
   KV android:label=@string/widget_name_3x1
   KV intent-filter ... /
   KV meta-data
   KV android:name=android.appwidget.provider
   KV android:resource=@xml/widget_info_3x1 /
   KV /receiver

   KV and then

   KV receiver android:name=.MyWidget_2x1
   KV android:label=@string/widget_name_2x1
   KV intent-filter ... /
   KV meta-data
   KV android:name=android.appwidget.provider
   KV android:resource=@xml/widget_info_2x1 /
   KV /receiver

   KV The key part is that this lets you reference two distinct
   KV xml/widget_info_3x1 and xml/widget_info_2x1.

   KV This is important because those specify android:minWidth and
   KV android:minHeight, which can't be changed at runtime.

Ah.  So even though I am defining multiple receivers, they all share
everything else?  Can the service know what kind of widget it's
updating?  Will they also share instances?  In other words, if install both a
2x1 widget and a 1x1 widget on the same homescreen, will it be
considered two instances of one widget or two different widgets?  I
would expect the answer to this makes a big difference as to how I do my
processing.

  2) How does one set options that are unique to a given
 instance?  The app currently uses SharedPreferences for all
 of its configuration.  But if both instances are running in
 the same context, how can each widget know how it is
 configured as distinct from another instance?

   KV Widget ids are persistent until a widget is removed. Store values
   KV in shared preferences (or some other persistence mechanism) keyed
   KV by widget id.


H.  Using that standard SharedPreferences, I did not see that it
could be keyed.  Did I miss that?  Each piece of configuration data is,
of course, keyed by the name of that piece of data.  But how would I
distinguish between the configuration data of two different widgets if
they are all stored in the same SharedPreferences?

   KV String key = Blah + String.valueOf(widgetId);
   KV prefs.getInt(key, 0);

   KV etc, etc etc.

  3) The app uses a service that is started by an alarm in order
 to do the updating.  When the alarm is triggered, the
 service calculates the time remaining until the event and
 then it updates the remote view.  Do both widgets share the
 same service?  I'd that is the case since it all one app
 with just multiple widget instances.  If that's the case,
 how can the service know which widget it is updating?

   KV Use

   KV AppWidgetManager.updateAppWidget(int widgetId, RemoteViews views)

   KV to update only one particular widget.

   KV You can get the ids when needed outside onUpdate like this:

   KV AppWidgetManager manager = AppWidgetManager.getInstance(context);
   KV ComponentName widgetClassName = new ComponentName(context,your
   WidgetProviderClass );
   KV int[] widgetIds = manager.getAppWidgetIds(widgetClassName);

   KV Be aware that in some cases, there may be widget ids that refer to
   KV non-existent widgets.

   KV I documented my workaround for this issue here:

   KV 
http://kmansoft.wordpress.com/2010/06/22/per-widget-options-stale-widgets/

And then the service would have to ask that instance which type of
data (sunrise or sunset) it should be updating.  But how to ask that
question?

   KV You can let the user specify this in the widget's configuration activity,
   KV then store that setting in SharedPrefs, and retrieve as needed.

   KV -- 
   KV Kostya Vasilyev -- http://kmansoft.wordpress.com

   KV -- 
   KV You received this message because you are subscribed to the Google
   KV Groups Android Developers group.
   KV To post to this group, send email to android-developers@googlegroups.com
   KV To unsubscribe from this group, send email to
   KV android-developers+unsubscr...@googlegroups.com
   KV For more options, visit this group at