最近突發奇想,想自己編一個Android電池插件放在桌面上,一是這個App確實有它的實用價值,二是編起來工程量應該不是很大,不用花太長時間,三來又能學習下Widget的開發方法,一舉三得,於是,暫停下游戲開發的學習,來編一個widget先。
在查找並結合多方資料後終於實現,效果圖如下:
長按桌面空白處,出現菜單,點擊Widgets,此時的插件已經裝入:
在電源連接時,機器人周圍會星光閃閃,表明正在充電,不在充電時,周圍的星光會消失。
機器人身上顯示電池電量百分比。
單擊機器人圖標,會跳出電池信息的詳情,再次單擊屏幕關閉詳情信息。
下面介紹代碼的實現:
整個工程主要實現兩個部分,一個是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(this, 0, alarmIntent, 0);
- AlarmManager alarm=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
- alarm.set(AlarmManager.RTC_WAKEUP,now+pause,pendingIntent);
-
- /** 更新AppWidget */
- manager.updateAppWidget(thisWidget, views);
-
- }
- }
- }