Android Open Source - robattery Widget Provider






From Project

Back to project page robattery.

License

The source code is released under:

GNU General Public License

If you think the Android project robattery listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
 * //from w  w  w .j  a  v a 2  s  .  c o  m
 */
package net.leetcode.robattery;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.widget.RemoteViews;

/**
 * @author jreese
 *
 */
public class WidgetProvider extends AppWidgetProvider {

  /**
   * Widgets that need to be updated
   */
  private static Map<Integer, Context> widgets = new HashMap<Integer, Context>();
  
  /**
   * 
   */
  public WidgetProvider() {
    // TODO Auto-generated constructor stub
  }
  
  public void onUpdate(Context context, AppWidgetManager manager, int[] ids) {
    for(int id : ids) {
      if(!widgets.containsKey(id)) {
        widgets.put(id, context);
      }
    }
  }

  public static void updateAll(Battery battery) {
    Set<Integer> keys = widgets.keySet();
    for(Integer id : keys) {
      Context context = widgets.get(id);
      AppWidgetManager manager = AppWidgetManager.getInstance(context);
      
      RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
      views.setTextViewText(R.id.widget_text, String.valueOf(battery.level) + "%");
      
      manager.updateAppWidget(id, views);
    }
  }

}




Java Source Code List

net.leetcode.robattery.BatteryServiceBinder.java
net.leetcode.robattery.BatteryService.java
net.leetcode.robattery.Battery.java
net.leetcode.robattery.BootReceiver.java
net.leetcode.robattery.Notify.java
net.leetcode.robattery.Robattery.java
net.leetcode.robattery.Settings.java
net.leetcode.robattery.WidgetProvider.java