Android中的电池小部件

时间:2011-11-09 09:59:29

标签: android android-widget

大家好我想用动画在android中制作一个电池小部件。我想我能够制作动画,但我想知道如何一次又一次地获得电池状态?通过线程可以做到吗?或者需要其他东西。这是动画的简单代码。请解释我如何一次又一次地从电池获取数据。我知道功能,但我不知道使用它们的机制。

    public class HelloWidget  extends AppWidgetProvider{

    RemoteViews remoteViews;
    AppWidgetManager appWidgetManager;
    ComponentName thisWidget;
    ImageView img;
    Bitmap icon, icon1;
    int counter = 0;

    @Override
    public void onUpdate(Context context, final AppWidgetManager appWidgetManager,
    int[] appWidgetIds) {



        this.appWidgetManager = appWidgetManager;
        remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
        thisWidget = new ComponentName(context, HelloWidget.class);
        remoteViews.setImageViewResource(R.id.imgv, R.drawable.icon2);
        appWidgetManager.updateAppWidget(thisWidget, remoteViews);


        ( new Thread() 
        {

            public void run()
            {

                while(true)
                {
                    if(counter%15>=0 && counter%15 <=7)
                    {
                        remoteViews.setImageViewResource(R.id.imgv, R.drawable.icon2);
                    }
                    else
                    {
                        remoteViews.setImageViewResource(R.id.imgv, R.drawable.icon);
                    }
                    appWidgetManager.updateAppWidget(thisWidget, remoteViews);
                    counter++;
                }

             }

        }

            ).start();




}

1 个答案:

答案 0 :(得分:0)

为电池事件注册广播接收器,不要每次都查询。

IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
mContext.registerReceiver(batteryLevelReceiver, batteryLevelFilter);