Android Widget 电池插件的开发实现

最近突发奇想,想自己编一个Android电池插件放在桌面上,一是这个App确实有它的实用价值,二是编起来工程量应该不是很大,不用花太长时间,三来又能学习下Widget的开发方法,一举三得,于是,暂停下游戏开发的学习,来编一个widget先。

在查找并结合多方资料后终于实现,效果图如下:
长按桌面空白处,出现菜单,点击Widgets,此时的插件已经装入:

 

Android Widget 电池插件的开发实现

在电源连接时,机器人周围会星光闪闪,表明正在充电,不在充电时,周围的星光会消失。

机器人身上显示电池电量百分比。

Android Widget 电池插件的开发实现


单击机器人图标,会跳出电池信息的详情,再次单击屏幕关闭详情信息。

Android Widget 电池插件的开发实现

下面介绍代码的实现:

Android Widget 电池插件的开发实现

整个工程主要实现两个部分,一个是AppWidget部分,实现桌面Widget的显示,更新等,另一个部分就是点击widget后出现的显示电池详细信息的Activity的实现了。

首先是AppWidget部分,上代码,NewBatteryWidget.java部分:

package com.ritterliu.newBatteryWidget;      import android.app.AlarmManager;   import android.app.PendingIntent;   import android.app.Service;   import android.appwidget.AppWidgetManager;   import android.appwidget.AppWidgetProvider;   import android.content.BroadcastReceiver;   import android.content.ComponentName;   import android.content.Context;   import android.content.Intent;   import android.content.IntentFilter;   import android.graphics.Bitmap;   import android.graphics.BitmapFactory;   import android.os.IBinder;   import android.widget.RemoteViews;      public class NewBatteryWidget extends AppWidgetProvider{           private static int currentBatteryLevel;       private static int currentBatteryStatus;          public void onUpdate(Context context,AppWidgetManager appWidgetManager,int[] appWidgetIds)       {           super.onUpdate(context, appWidgetManager, appWidgetIds);                  /** 启动自动更新电池信息的service */           context.startService(new Intent(context,updateService.class));                  /** 为AppWidget设置点击事件的响应,启动显示电池信息详情的activity */            Intent startActivityIntent = new Intent(context,NewBatteryInfoActivity.class);           PendingIntent Pintent = PendingIntent.getActivity(context,0,startActivityIntent,0);           RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.newrelativelayout);           views.setOnClickPendingIntent(R.id.imageView,Pintent);           appWidgetManager.updateAppWidget(appWidgetIds,views);                      }              /** 自动更新电池信息的service,通过AlarmManager实现定时不间断地发送电池信息 */       public static class updateService extends Service{           Bitmap bmp;     //定义机器人图片            @Override           public IBinder onBind(Intent intent) {               // TODO Auto-generated method stub                return null;           }                      /** 定义一个接收电池信息的broascastReceiver */           private BroadcastReceiver batteryReceiver=new BroadcastReceiver()           {               @Override               public void onReceive(Context context, Intent intent) {                   // TODO Auto-generated method stub                    currentBatteryLevel=intent.getIntExtra("level"0);                   currentBatteryStatus=intent.getIntExtra("status"0);               }                          };                                 public void onStart(Intent intent,int startId)           {               super.onStart(intent, startId);                              /** 注册接收器 */               registerReceiver(batteryReceiver,new IntentFilter(Intent.ACTION_BATTERY_CHANGED));                              /** 定义一个AppWidgetManager */               AppWidgetManager manager=AppWidgetManager.getInstance(this);                              /** 定义一个RemoteViews,实现对AppWidget界面控制 */               RemoteViews views=new RemoteViews(getPackageName(),R.layout.newrelativelayout);                                             if(currentBatteryStatus==2||currentBatteryStatus==5)    //当正在充电或充满电时,显示充电的图片                {                   if(currentBatteryLevel>=95)                   {                       bmp=BitmapFactory.decodeResource(getResources(),R.drawable.jcharge);                   }                   else if(currentBatteryLevel>=85&& currentBatteryLevel<95)                   {                       bmp=BitmapFactory.decodeResource(getResources(),R.drawable.icharge);                   }                   else if(currentBatteryLevel>=75&& currentBatteryLevel<85)                   {                       bmp=BitmapFactory.decodeResource(getResources(),R.drawable.hcharge);                   }                   else if(currentBatteryLevel>=65&& currentBatteryLevel<75)                   {                       bmp=BitmapFactory.decodeResource(getResources(),R.drawable.gcharge);                   }                   else if(currentBatteryLevel>=55&& currentBatteryLevel<65)                   {                       bmp=BitmapFactory.decodeResource(getResources(),R.drawable.fcharge);                   }                   else if(currentBatteryLevel>=45&& currentBatteryLevel<55)                   {                       bmp=BitmapFactory.decodeResource(getResources(),R.drawable.echarge);                       }                   else if(currentBatteryLevel>=35&& currentBatteryLevel<45)                   {                       bmp=BitmapFactory.decodeResource(getResources(),R.drawable.dcharge);                       }                   else if(currentBatteryLevel>=25&& currentBatteryLevel<35)                   {                       bmp=BitmapFactory.decodeResource(getResources(),R.drawable.ccharge);                       }                   else if(currentBatteryLevel>=15&& currentBatteryLevel<25)                   {                       bmp=BitmapFactory.decodeResource(getResources(),R.drawable.bcharge);                   }                   else                   {                       bmp=BitmapFactory.decodeResource(getResources(),R.drawable.acharge);                       }                  }               else    //未在充电时,显示不在充电状态的系列图片                {                   if(currentBatteryLevel>=95)                   {                       bmp=BitmapFactory.decodeResource(getResources(),R.drawable.j);                   }                   else if(currentBatteryLevel>=85&¤tBatteryLevel<95)                   {                       bmp=BitmapFactory.decodeResource(getResources(),R.drawable.i);                   }                   else if(currentBatteryLevel>=75&¤tBatteryLevel<85)                   {                       bmp=BitmapFactory.decodeResource(getResources(),R.drawable.h);                   }                   else if(currentBatteryLevel>=65&¤tBatteryLevel<75)                   {                       bmp=BitmapFactory.decodeResource(getResources(),R.drawable.g);                   }                   else if(currentBatteryLevel>=55&¤tBatteryLevel<65)                   {                       bmp=BitmapFactory.decodeResource(getResources(),R.drawable.f);                   }                   else if(currentBatteryLevel>=45&¤tBatteryLevel<55)                   {                       bmp=BitmapFactory.decodeResource(getResources(),R.drawable.e);                     }                   else if(currentBatteryLevel>=35&¤tBatteryLevel<45)                   {                       bmp=BitmapFactory.decodeResource(getResources(),R.drawable.d);                     }                   else if(currentBatteryLevel>=25&¤tBatteryLevel<35)                   {                       bmp=BitmapFactory.decodeResource(getResources(),R.drawable.c);                     }                   else if(currentBatteryLevel>=15&¤tBatteryLevel<25)                   {                       bmp=BitmapFactory.decodeResource(getResources(),R.drawable.b);                   }                   else                   {                       bmp=BitmapFactory.decodeResource(getResources(),R.drawable.a);                     }                  }                     /** 设置AppWidget上显示的图片和文字的内容 */                   views.setImageViewBitmap(R.id.imageView,bmp);               views.setTextViewText(R.id.tv,currentBatteryLevel+"%");                              ComponentName thisWidget=new ComponentName(this,NewBatteryWidget.class);                          /** 使用AlarmManager实现每隔一秒发送一次更新提示信息,实现信息实时动态变化 */               long now=System.currentTimeMillis();               long pause=1000;                              Intent alarmIntent=new Intent();               alarmIntent=intent;                              PendingIntent pendingIntent=PendingIntent.getService(this0, alarmIntent, 0);               AlarmManager alarm=(AlarmManager)getSystemService(Context.ALARM_SERVICE);               alarm.set(AlarmManager.RTC_WAKEUP,now+pause,pendingIntent);                              /** 更新AppWidget */                           manager.updateAppWidget(thisWidget, views);              }          }   }  

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wysfwg.html