3

I am working on a battery widget, so when ever Intent.ACTION_BATTERY_CHANGED occurs it is received by widget onReceive() method from where I can get its data now the problem is this, that this whole procedure goes find fine but when time passes means you can say widget gets live round about can say 4 to 5 hours and Widget stops update, at the moment I am using textView to show the data of battery but after some hours widget stops updating data in the textView here is the code

here is the update method of widget

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {

    context.getApplicationContext().registerReceiver(this,
            new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

    ComponentName cn =
        new ComponentName(context, BatteryInfoActivity.class);
    appWidgetManager.updateAppWidget(cn, this.views);
}

here is the on onRecive method

    @Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
        //Integer level = intent.getIntExtra("level", -1);

        String mgs = "--";
        //String BatterInfo = "Level"+ intent.getIntExtra("level", -1)+ "/" + intent.getIntExtra("scale", -1);
        Integer level = intent.getIntExtra("level", -1);
        Integer scale = intent.getIntExtra("scale", -1);
        Integer temp = intent.getIntExtra("temperature",-1);
        Integer voltage = intent.getIntExtra("voltage", -1);
        Integer health = intent.getIntExtra("health", -1);
          boolean present = intent.getBooleanExtra("present", false);
          Integer plugged = intent.getIntExtra("plugged", -1);
          String technology = intent.getStringExtra("technology");
          Integer status = intent.getIntExtra("status",-1);      
          mgs = mgs + " Technology " + technology + "--";
          mgs = mgs + "Battery is Present " + present+"--";
          mgs = mgs + " level "+ level + "/" +  scale + "--";
          mgs = mgs + "tempuratue" + temp + "--";
          mgs = mgs + "voltage"+ voltage + "--"; 

        this.views.setTextViewText(R.id.textView1, mgs);
        ComponentName cn =
            new ComponentName(context, BatteryInfoActivity.class);
        AppWidgetManager.getInstance(context).updateAppWidget(cn, this.views);
    }
    super.onReceive(context, intent);
}
ppreetikaa
  • 1,149
  • 2
  • 15
  • 22
mfq
  • 1,357
  • 13
  • 21

1 Answers1

0

You cannot register a BroadcastReceiver from another BroadcastReceiver this way. You cannot do much of anything in onReceive() that would live past the end of onReceive() itself.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491