本文介绍了电池小部件的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

大家好我想在动画的android电池部件。我想我就能动画,但我想知道,我怎么能一次又一次的让电池状态?这将是确定通过线程办呢?还是其他什么东西是必需的。下面是简单的code动画。请解释我是如何将得到的电池数据连连所有再寄一次知道这个功能,但我不知道使用它们的机制。

 公共类HelloWidget的扩展AppWidgetProvider {    RemoteViews remoteViews;
    AppWidgetManager appWidgetManager;
    组件名thisWidget;
    ImageView的IMG;
    位图图标,ICON1;
    INT计数器= 0;    @覆盖
    公共无效的onUpdate(上下文的背景下,最终AppWidgetManager appWidgetManager,
    INT [] appWidgetIds){        this.appWidgetManager = appWidgetManager;
        remoteViews =新RemoteViews(context.getPackageName(),R.layout.main);
        thisWidget =新的组件名(背景下,HelloWidget.class);
        remoteViews.setImageViewResource(R.id.imgv,R.drawable.icon2);
        appWidgetManager.updateAppWidget(thisWidget,remoteViews);
        (新的Thread()
        {            公共无效的run()
            {                而(真)
                {
                    如果(计数器%15> = 0&放大器;&安培;计数器%15℃; = 7)
                    {
                        remoteViews.setImageViewResource(R.id.imgv,R.drawable.icon2);
                    }
                    其他
                    {
                        remoteViews.setImageViewResource(R.id.imgv,R.drawable.icon);
                    }
                    appWidgetManager.updateAppWidget(thisWidget,remoteViews);
                    反++;
                }             }        }            )。开始();
}


解决方案

注册广播接收器电池事件,并且不查询每一次。

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

Hi all I want to make a battery widget in android with animation. I think I will be able to animate but I want to know that how can I get the battery status again and again? Will it be OK to do it through thread? Or something else is required. Here is the simple code for animation. Please explain how will I get the data from battery again and again all the time.I know the functions but I don't know the mechanism to use them.

    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();




}
解决方案

Register broadcast receiver for battery events and don't query every time.

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

这篇关于电池小部件的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 19:04