android小部件停止几小时后更新代码?

时间:2011-11-24 19:28:59

标签: android widget battery

我正在开发一个电池小部件,所以当Intent.ACTION_BATTERY_CHANGED出现时,它会被widget onRecive方法从中获取,我现在可以获取它的数据问题就是这个,整个过程会很好找到,但是当时间过去意味着你可以说小部件实时播放可以说4到5个小时并且小部件停止更新,此刻我正在使用textveiw来显示电池数据但是在几个小时后小部件停止更新文本中的数据这里是代码

这是widget的更新方法         @覆盖     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);
}

这是on onRecive方法

    @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);
}

1 个答案:

答案 0 :(得分:0)

您不能以这种方式从另一个BroadcastReceiver注册BroadcastReceiveronReceive()onReceive()本身的任何内容都无法完成。