我处于充电状态时无法更新小部件。电池电量保持不变。我的代码出了什么问题?
public class BatteryStatusWidgetActivity extends AppWidgetProvider {
/** Called when the activity is first created. */
private String batteryLevel = "init";
private String temperatura = "init";
private int widgetImageFrame = R.drawable.full;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
context.getApplicationContext().registerReceiver(this,
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
updateView(context);
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().contentEquals(
"com.sec.android.widgetapp.APPWIDGET_RESIZE")
|| intent.getAction().equals(
"android.appwidget.action.APPWIDGET_UPDATE_OPTIONS")) {
} else {
int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
int level = -1;
level = (rawlevel * 100) / scale;
batteryLevel = level + "%";
// temperatura
int temperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,
-1) / 10;
temperatura = temperature + "°C";
widgetImageFrame = R.drawable.full;
updateView(context);
}
super.onReceive(context, intent);
}
public void updateView(Context context) {
RemoteViews thisViews = new RemoteViews(context.getApplicationContext()
.getPackageName(), R.layout.widget_layout);
thisViews.setTextViewText(R.id.widget_text, batteryLevel + " | "
+ temperatura);
thisViews.setImageViewResource(R.id.imageView1, widgetImageFrame);
ComponentName thisWidget = new ComponentName(context,
BatteryStatusWidgetActivity.class);
AppWidgetManager.getInstance(context).updateAppWidget(thisWidget,
thisViews);
}
}
这是小部件的主要内容
<?xml version="1.0" encoding="utf-8" ?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:initialLayout="@layout/widget_layout" android:minWidth="40dp" android:minHeight="40dp" android:updatePeriodMillis="3000" />
那么问题是什么? android:updatePeriodMillis="3000"
?
答案 0 :(得分:0)
updatePeriodMillis 属性定义App Widget框架应通过调用onUpdate()
回调方法从AppWidgetProvider请求更新的频率。
来自http://developer.android.com/guide/topics/appwidgets/index.html#MetaData