android小部件几小时后停止更新代码?
我正在研究一个电池小部件,所以当Intent.ACTION_BATTERY_CHANGED发生时,它会被小部件onRecive方法从我可以从中获取其数据的地方收回问题是这样的,整个过程很顺利,但是当时间流逝意味着你可以说小部件获得生活周围可以说4至5小时,Widget停止更新,此刻我使用textveiw来显示电池的数据,但一些小时后,部件停止更新text文件中的数据这里是代码
这里是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);
}
这里是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);
}
您无法以这种方式从另一个BroadcastReceiver
注册BroadcastReceiver
。 你不能在onReceive()
中做任何事情,这会超过onReceive()
本身的结尾。