Those broadcast actions are definitely what you want:

http://developer.android.com/reference/android/net/wifi/WifiManager.html

Then just register for them in your widgets manifest, for example:

<intent-filter>
      <action
android:name="android.appwidget.action.APPWIDGET_UPDATE" />
      <action
android:name="android.net.wifi.WifiManager.NETWORK_STATE_CHANGED_ACTION" /
>

Then override your AppWidget's onReceive, and handle that Intent when
it arrives:

public void onReceive(Context context, Intent intent)
{
     String action = intent.getAction();
     if (action!=null &&
action==android.net.wifi.WifiManager.NETWORK_STATE_CHANGED_ACTION)
     {
          //.. handle it here, using remote views to update the widget
     }

For what its worth, I wouldn't be too pessimistic about the scheduling
periodic updates and battery life, there is a balance to be found.
I had a widget update every minute, after extensive testing I noticed
almost no difference to the battery life on my Hero. Every second or
10 seconds might be a different matter, however.

But registering for just the broadcast events you need to act on is a
far better approach than periodic polling, if you can get away with
it.

For the second part of your requirement, once you have calculated the
Quality of Sound in your service, you can do a similar thing, and
broadcast an Intent directly to your AppWidget, the Intent could
contain the QoS data as extras:

In your service:
        Intent i = new Intent(this,YourWidget.class);
        i.setAction(YourWidget.UPDATE_QOS);
        // put your QoS data in i as extras..
        sendBroadcast(i);

Where UPDATE_QOS is a public string in your appwidget that defines
your action.

Then handle that again in your widget's onReceive:

public void onReceive(Context context, Intent intent)
{
     String action = intent.getAction();
     if (action!=null &&
action==android.net.wifi.WifiManager.NETWORK_STATE_CHANGED_ACTION)
     {
          //.. handle it here, using remote views to update the widget
     }
     else if (action!=null && action==UPDATE_QOS)
     {
          //.. handle your QOS update here, extracting the data from
the Intent's extras, and using remote views to update the widget
     }

Then you just have to consider how often your service broadcasts this
QOS update message.
Firstly only broadcast an update when your QOS has actually changed,
rather than on a periodic basis. There is no point in sending an
update if the level is not actually changing.
Secondly look into how your QoS is calculated, does it work out the
QoS itself by polling based on a Timer or AlarmManager, or could you
also process the wifi changed actions, and again only work out the
level if something has caused it to change?

Thirdly, you might want to include a button on your widget to switch
on or off some or all of the features, so that for example if they are
not actively interested in your QoS, then the QOS service updates
don't get claculated and sent.


Regards
James

On May 29, 6:05 am, Kostya Vasilyev <[email protected]> wrote:
> There are Wifi-specific broadcast actions you can register your widget for,
> and only update when there is a change. Thesw include: a change in signal
> level, connection state, etc.
>
> This way your code can avoid burning through the battery and still display
> the information you want.
>
> Also, take a look here:
>
> http://kmansoft.wordpress.com/sw
>
> 29.05.2010 1:53 пользователь "Dirk Vranckaert" <[email protected]>
> написал:
>
> I think String is right about that. I wouldn't do that either just to
> save battery life!
>
> However I think it is better to set the update interval of the applet
> to a high enough amount of time (like 24 hours or sth) and just update
> the widget manually. I'm currently working on a widget to and in my
> case the widget only receives an update upon clicking a certain
> button.
>
> The way I do that is like this:
>
> RemoteViews rv = new RemoteViews(context.getPackageName(), WIDGET);
> rv.setTextViewText(R.id.textField, "Updated value for some text on the
> widget");
> ComponentName cn = new ComponentName(context, YourActivity.class);
> AppWidgetManager mgr = AppWidgetManager.getInstance(context);
> mgr.updateAppWidget(cn, rv);
>
> That should basically work to update a widget.
> Oh and one tip: using the this method you should do all updating just
> after the line "rv.setTextViewText(...)". If you execute this entire
> block a few times after each other you can get into troubles when
> updating the widget.
>
> Dirk
>
> On 28 mei, 23:40, String <[email protected]> wrote:
>
>
>
> > Before you get too far into this, you ...- Hide quoted text -
>
> - Show quoted text -

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

Reply via email to