android widget stop update the code after few hours?

I am working on a battery widget, so when ever Intent.ACTION_BATTERY_CHANGED occurs it is recived by widget onRecive 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 u can say widget gets live round about can say 4 to 5 hours and Widget stops update, at the moment i am using textveiw to show the data of battery but after some hours widget stops updating data in the textVeiw 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);
}

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.

链接地址: http://www.djcxy.com/p/50618.html

上一篇: TextView没有在android应用部件中得到更新

下一篇: android小部件几小时后停止更新代码?