歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android Widget 電池插件的開發實現

最近突發奇想,想自己編一個Android電池插件放在桌面上,一是這個App確實有它的實用價值,二是編起來工程量應該不是很大,不用花太長時間,三來又能學習下Widget的開發方法,一舉三得,於是,暫停下游戲開發的學習,來編一個widget先。

在查找並結合多方資料後終於實現,效果圖如下:
長按桌面空白處,出現菜單,點擊Widgets,此時的插件已經裝入:

 

在電源連接時,機器人周圍會星光閃閃,表明正在充電,不在充電時,周圍的星光會消失。

機器人身上顯示電池電量百分比。


單擊機器人圖標,會跳出電池信息的詳情,再次單擊屏幕關閉詳情信息。

下面介紹代碼的實現:

整個工程主要實現兩個部分,一個是AppWidget部分,實現桌面Widget的顯示,更新等,另一個部分就是點擊widget後出現的顯示電池詳細信息的Activity的實現了。

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

  1. package com.ritterliu.newBatteryWidget;  
  2.   
  3. import android.app.AlarmManager;  
  4. import android.app.PendingIntent;  
  5. import android.app.Service;  
  6. import android.appwidget.AppWidgetManager;  
  7. import android.appwidget.AppWidgetProvider;  
  8. import android.content.BroadcastReceiver;  
  9. import android.content.ComponentName;  
  10. import android.content.Context;  
  11. import android.content.Intent;  
  12. import android.content.IntentFilter;  
  13. import android.graphics.Bitmap;  
  14. import android.graphics.BitmapFactory;  
  15. import android.os.IBinder;  
  16. import android.widget.RemoteViews;  
  17.   
  18. public class NewBatteryWidget extends AppWidgetProvider{      
  19.     private static int currentBatteryLevel;  
  20.     private static int currentBatteryStatus;  
  21.   
  22.     public void onUpdate(Context context,AppWidgetManager appWidgetManager,int[] appWidgetIds)  
  23.     {  
  24.         super.onUpdate(context, appWidgetManager, appWidgetIds);  
  25.       
  26.         /** 啟動自動更新電池信息的service */  
  27.         context.startService(new Intent(context,updateService.class));  
  28.       
  29.         /** 為AppWidget設置點擊事件的響應,啟動顯示電池信息詳情的activity */   
  30.         Intent startActivityIntent = new Intent(context,NewBatteryInfoActivity.class);  
  31.         PendingIntent Pintent = PendingIntent.getActivity(context,0,startActivityIntent,0);  
  32.         RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.newrelativelayout);  
  33.         views.setOnClickPendingIntent(R.id.imageView,Pintent);  
  34.         appWidgetManager.updateAppWidget(appWidgetIds,views);  
  35.               
  36.     }  
  37.       
  38.     /** 自動更新電池信息的service,通過AlarmManager實現定時不間斷地發送電池信息 */  
  39.     public static class updateService extends Service{  
  40.         Bitmap bmp;     //定義機器人圖片   
  41.         @Override  
  42.         public IBinder onBind(Intent intent) {  
  43.             // TODO Auto-generated method stub   
  44.             return null;  
  45.         }  
  46.           
  47.         /** 定義一個接收電池信息的broascastReceiver */  
  48.         private BroadcastReceiver batteryReceiver=new BroadcastReceiver()  
  49.         {  
  50.             @Override  
  51.             public void onReceive(Context context, Intent intent) {  
  52.                 // TODO Auto-generated method stub   
  53.                 currentBatteryLevel=intent.getIntExtra("level"0);  
  54.                 currentBatteryStatus=intent.getIntExtra("status"0);  
  55.             }  
  56.               
  57.         };  
  58.           
  59.           
  60.         public void onStart(Intent intent,int startId)  
  61.         {  
  62.             super.onStart(intent, startId);  
  63.               
  64.             /** 注冊接收器 */  
  65.             registerReceiver(batteryReceiver,new IntentFilter(Intent.ACTION_BATTERY_CHANGED));  
  66.               
  67.             /** 定義一個AppWidgetManager */  
  68.             AppWidgetManager manager=AppWidgetManager.getInstance(this);  
  69.               
  70.             /** 定義一個RemoteViews,實現對AppWidget界面控制 */  
  71.             RemoteViews views=new RemoteViews(getPackageName(),R.layout.newrelativelayout);  
  72.               
  73.               
  74.             if(currentBatteryStatus==2||currentBatteryStatus==5)    //當正在充電或充滿電時,顯示充電的圖片   
  75.             {  
  76.                 if(currentBatteryLevel>=95)  
  77.                 {  
  78.                     bmp=BitmapFactory.decodeResource(getResources(),R.drawable.jcharge);  
  79.                 }  
  80.                 else if(currentBatteryLevel>=85&& currentBatteryLevel<95)  
  81.                 {  
  82.                     bmp=BitmapFactory.decodeResource(getResources(),R.drawable.icharge);  
  83.                 }  
  84.                 else if(currentBatteryLevel>=75&& currentBatteryLevel<85)  
  85.                 {  
  86.                     bmp=BitmapFactory.decodeResource(getResources(),R.drawable.hcharge);  
  87.                 }  
  88.                 else if(currentBatteryLevel>=65&& currentBatteryLevel<75)  
  89.                 {  
  90.                     bmp=BitmapFactory.decodeResource(getResources(),R.drawable.gcharge);  
  91.                 }  
  92.                 else if(currentBatteryLevel>=55&& currentBatteryLevel<65)  
  93.                 {  
  94.                     bmp=BitmapFactory.decodeResource(getResources(),R.drawable.fcharge);  
  95.                 }  
  96.                 else if(currentBatteryLevel>=45&& currentBatteryLevel<55)  
  97.                 {  
  98.                     bmp=BitmapFactory.decodeResource(getResources(),R.drawable.echarge);      
  99.                 }  
  100.                 else if(currentBatteryLevel>=35&& currentBatteryLevel<45)  
  101.                 {  
  102.                     bmp=BitmapFactory.decodeResource(getResources(),R.drawable.dcharge);      
  103.                 }  
  104.                 else if(currentBatteryLevel>=25&& currentBatteryLevel<35)  
  105.                 {  
  106.                     bmp=BitmapFactory.decodeResource(getResources(),R.drawable.ccharge);      
  107.                 }  
  108.                 else if(currentBatteryLevel>=15&& currentBatteryLevel<25)  
  109.                 {  
  110.                     bmp=BitmapFactory.decodeResource(getResources(),R.drawable.bcharge);  
  111.                 }  
  112.                 else  
  113.                 {  
  114.                     bmp=BitmapFactory.decodeResource(getResources(),R.drawable.acharge);      
  115.                 }     
  116.             }  
  117.             else    //未在充電時,顯示不在充電狀態的系列圖片   
  118.             {  
  119.                 if(currentBatteryLevel>=95)  
  120.                 {  
  121.                     bmp=BitmapFactory.decodeResource(getResources(),R.drawable.j);  
  122.                 }  
  123.                 else if(currentBatteryLevel>=85&¤tBatteryLevel<95)  
  124.                 {  
  125.                     bmp=BitmapFactory.decodeResource(getResources(),R.drawable.i);  
  126.                 }  
  127.                 else if(currentBatteryLevel>=75&¤tBatteryLevel<85)  
  128.                 {  
  129.                     bmp=BitmapFactory.decodeResource(getResources(),R.drawable.h);  
  130.                 }  
  131.                 else if(currentBatteryLevel>=65&¤tBatteryLevel<75)  
  132.                 {  
  133.                     bmp=BitmapFactory.decodeResource(getResources(),R.drawable.g);  
  134.                 }  
  135.                 else if(currentBatteryLevel>=55&¤tBatteryLevel<65)  
  136.                 {  
  137.                     bmp=BitmapFactory.decodeResource(getResources(),R.drawable.f);  
  138.                 }  
  139.                 else if(currentBatteryLevel>=45&¤tBatteryLevel<55)  
  140.                 {  
  141.                     bmp=BitmapFactory.decodeResource(getResources(),R.drawable.e);    
  142.                 }  
  143.                 else if(currentBatteryLevel>=35&¤tBatteryLevel<45)  
  144.                 {  
  145.                     bmp=BitmapFactory.decodeResource(getResources(),R.drawable.d);    
  146.                 }  
  147.                 else if(currentBatteryLevel>=25&¤tBatteryLevel<35)  
  148.                 {  
  149.                     bmp=BitmapFactory.decodeResource(getResources(),R.drawable.c);    
  150.                 }  
  151.                 else if(currentBatteryLevel>=15&¤tBatteryLevel<25)  
  152.                 {  
  153.                     bmp=BitmapFactory.decodeResource(getResources(),R.drawable.b);  
  154.                 }  
  155.                 else  
  156.                 {  
  157.                     bmp=BitmapFactory.decodeResource(getResources(),R.drawable.a);    
  158.                 }     
  159.             }     
  160.   
  161.             /** 設置AppWidget上顯示的圖片和文字的內容 */  
  162.                 views.setImageViewBitmap(R.id.imageView,bmp);  
  163.             views.setTextViewText(R.id.tv,currentBatteryLevel+"%");  
  164.               
  165.             ComponentName thisWidget=new ComponentName(this,NewBatteryWidget.class);  
  166.           
  167.             /** 使用AlarmManager實現每隔一秒發送一次更新提示信息,實現信息實時動態變化 */  
  168.             long now=System.currentTimeMillis();  
  169.             long pause=1000;  
  170.               
  171.             Intent alarmIntent=new Intent();  
  172.             alarmIntent=intent;  
  173.               
  174.             PendingIntent pendingIntent=PendingIntent.getService(this0, alarmIntent, 0);  
  175.             AlarmManager alarm=(AlarmManager)getSystemService(Context.ALARM_SERVICE);  
  176.             alarm.set(AlarmManager.RTC_WAKEUP,now+pause,pendingIntent);  
  177.               
  178.             /** 更新AppWidget */  
  179.                         manager.updateAppWidget(thisWidget, views);  
  180.   
  181.         }     
  182.     }  
  183. }  
Copyright © Linux教程網 All Rights Reserved