title: Battery widget stop updating after some time
body: <p>I am trying to make a battery widget which will show the battery level. When it starts, it works fine but after some time, it stops working and start showing Battery level 0. Please help me to figuring out that what I'm missing.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>public class Widget extends AppWidgetProvider {
      static int level;
  static int powers;
  AppWidgetManager ap;
  
  @Override
  public void onReceive(final Context context, Intent intent) {
      // TODO Auto-generated method stub
      super.onReceive(context, intent);
  
      System.out.println(intent.getAction());
  
      if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BATTERY_CHANGED)) {
  
          int rawlevel = intent.getIntExtra("level", -1);
          int scale = intent.getIntExtra("scale", -1);
  
          powers = intent.getIntExtra("plugged", -1);
  
          level = -1;
          if (rawlevel &gt;= 0 &amp;&amp; scale &gt; 0) {
              level = (rawlevel * 100) / scale;
          }
          onUpdatew(context);
  
      }
  }
  
  @Override
  public void onUpdate(Context context, AppWidgetManager appWidgetManager,
          int[] appWidgetIds) {
      final int N = appWidgetIds.length;
      for (int i = 0; i &lt; N; i++) {
          int appWidgetId = appWidgetIds[0];
  
          Intent intent = new Intent(context, xyz.class);
          PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                  intent, 0);
  
          RemoteViews views = new RemoteViews(context.getPackageName(),
                  R.layout.widget_main);
  
          views.setTextViewText(R.id.txt1, level + "");
          Log.i("Power", powers + "");
          Log.i("Level", level + "");
  
          views.setOnClickPendingIntent(R.id.iv, pendingIntent);
          appWidgetManager.updateAppWidget(appWidgetId, views);
  
          super.onUpdate(context, appWidgetManager, appWidgetIds);
      }
  
  }
  
  @Override
  public void onEnabled(Context context) {
      // TODO Auto-generated method stub
      System.out.println(context.getApplicationContext().registerReceiver(
              this, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)));
      onUpdatew(context);
      super.onEnabled(context);
  }
  
  public void onUpdatew(Context context) {
      RemoteViews views = new RemoteViews(context.getPackageName(),
              R.layout.widget_main);
  
      views.setTextViewText(R.id.txt1, level + "");
  
      AppWidgetManager myManager = AppWidgetManager.getInstance(context);
      ComponentName myName = new ComponentName(context, Widget.class);
      myManager.updateAppWidget(myName, views);
  
  }}
  </code></pre>