0

处于充电状态时,我无法更新小部件。电池电量保持不变。我的代码有什么问题?

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"?

4

1 回答 1

0

updatePeriodMillis属性定义了 App Widget 框架通过调用onUpdate()回调方法向 AppWidgetProvider 请求更新的频率。

来自 http://developer.android.com/guide/topics/appwidgets/index.html#MetaData

于 2013-08-22T08:58:14.607 回答