你好,游客 登录 注册 搜索
背景:
阅读新闻

Android Widget 电池插件的开发实现

[日期:2011-11-15] 来源:csdn  作者:ritterliu [字体:大 中 小]
对于Widget,配置它的显示layout,一个简单的RelativeLayout布局,一个ImageView和一个TextView,居中显示:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:Android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent" >  
  5.    
  6.         <ImageView   
  7.         android:id="@+id/imageView"  
  8.         android:layout_centerInParent="true"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:src="@drawable/j"  
  12.         />  
  13.   
  14.         <TextView  
  15.         android:id="@+id/tv"  
  16.         android:layout_centerInParent="true"  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:textColor="#000000"  
  20.         android:textStyle="bold"  
  21.         android:textSize="14sp"  
  22.         />  
  23.   
  24. </RelativeLayout>
接着就是编写配置Widget的xml了,设置Widget大小等信息,在res目录下新建一个xml文件夹用来存放Widget的配置xml文件://备注2
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:minHeight="72dip"  
  4.     android:minWidth="72dip"  
  5.     android:updatePeriodMillis="1000000"      
  6.     android:initialLayout="@layout/newrelativelayout"  
  7.     >  
  8.     <!--  
  9.     关于android:minHeight和android:minWidth  
  10.     分别对应appWidget在屏幕上所占位置的高和宽,  
  11.     最小高和宽各为一个单元格,值为72dip,  
  12.     有资料说计算公式为(74*N)-2  
  13.     例如要设置宽为四个单元格时,(74*4)-2=294  
  14.     android:minWidth="294dip"  
  15.       
  16.     注意,看网上资料说,在SDK1.5之后,  
  17.     android:updatePeriodMillis就没用了,  
  18.     不会再定时更新appWidget了,所以这里的值  
  19.     设置多少都不会有影响,但是最好设置大一点,  
  20.     防止万一又有效了,更新的太频繁会不好。  
  21.     -->  
  22. </appwidget-provider>
备注1,备注2:

请注意,在一些资料上说widget配置文件new_battery_widget.xml中的android:updatePeriodMillis是用来实现widget自动更新的,但本人编程时却发现,���个设置根本就没有效果,后来上网一搜,人家说这个功能在SDK1.5以后就不支持了。所以本次程序自动更新响应系统的battery change事件是通过一个AlarmManager定时发送响应来实现的,同学们千万注意,别像我一样一开始等着靠android:updatePeriodMillis实现更新,看没效果,还以为是别的什么地方出了问题,浪费了不少时间。

Widget的部分差不多了,下面介绍显示电池详情的Activity部分,上代码:

  1. package com.ritterliu.newBatteryWidget;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.Service;  
  5. import android.content.BroadcastReceiver;  
  6. import android.content.Context;  
  7. import android.content.Intent;  
  8. import android.content.IntentFilter;  
  9. import android.os.Bundle;  
  10. import android.os.IBinder;  
  11. import android.view.MotionEvent;  
  12. import android.view.Window;  
  13. import android.widget.TextView;  
  14.   
  15. public class NewBatteryInfoActivity extends Activity{  
  16.     /** 定义电池信息变量  */  
  17.     private static int currentBatteryPlugged=0;  
  18.     private static int currentBatteryStatus=0;  
  19.     private static int currentBatteryLevel=0;  
  20.     private static int currentBatteryHealth=0;  
  21.     private static int currentBatteryTemperature=0;  
  22.     private static int currentBatteryVoltage=0;  
  23.     private static String currentBatteryTechnology="";  
  24.   
  25.       
  26.     /** TextView 声明 */  
  27.     private static TextView tvBatteryStatus;  
  28.     private static TextView tvBatteryLevel;  
  29.     private static TextView tvBatteryHealth;  
  30.     private static TextView tvBatteryTemperature;  
  31.     private static TextView tvBatteryVoltage;  
  32.     private static TextView tvBatteryTechnology;  
  33.       
  34.     /** 定义好字符串以备使用 */  
  35.     private static String batteryStatus="电池状态: ";  
  36.     private static String batteryLevel="电池电量: ";  
  37.     private static String batteryHealth="电池健康: ";  
  38.     private static String batteryTemperature="电池温度: ";  
  39.     private static String batteryVoltage="电池电压: ";  
  40.     private static String  batteryTechnology="电池技术: ";  
  41.       
  42.     private static String  batteryStatusCharging="正在充电";  
  43.     private static String  batteryStatusDischarging="正在放电";  
  44.     private static String  batteryStatusFull="已充满";  
  45.     private static String  batteryStatusNotCharging="未在充电";  
  46.     private static String  batteryStatusUnknown="状态未知";  
  47.           
  48.     private static String  batteryPluggedAC="(AC)";  
  49.     private static String  batteryPluggedUSB="(USB)";  
  50.           
  51.     private static String  batteryHealthCold="过冷";  
  52.     private static String  batteryHealthDead="损坏";  
  53.     private static String  batteryHealthGood="良好";  
  54.     private static String  batteryHealthOverheat="过热";  
  55.     private static String  batteryHealthOverVoltage="过压";  
  56.     private static String  batteryHealthUnknown="未知";  
  57.     private static String  batteryHealthUnspecifiedFailure="未知的故障";  
  58.       
  59.     /** 提示Service启动标志位 */  
  60.     private static boolean flag;  
  61.       
  62.     /** 提示信息接收器 */  
  63.     BroadcastReceiver infoReceiver;  
  64.       
  65.     public void onCreate(Bundle savedInstanceState)  
  66.     {  
  67.         super.onCreate(savedInstanceState);  
  68.           
  69.         this.requestWindowFeature(Window.FEATURE_NO_TITLE);     //设置activity无标题   
  70.         setContentView(R.layout.newlayout);     //使用newlayout的布局   
  71.           
  72.         tvBatteryStatus=(TextView)findViewById(R.id.tvBatteryStatus);  
  73.         tvBatteryLevel=(TextView)findViewById(R.id.tvBatteryLevel);  
  74.         tvBatteryHealth=(TextView)findViewById(R.id.tvBatteryHealth);  
  75.         tvBatteryTemperature=(TextView)findViewById(R.id.tvBatteryTemperature);  
  76.         tvBatteryVoltage=(TextView)findViewById(R.id.tvBatteryVoltage);  
  77.         tvBatteryTechnology=(TextView)findViewById(R.id.tvBatteryTechnology);  
  78.           
  79.         flag=true;      //提示service的标志位置为true   
  80.           
  81.         infoReceiver=new BroadcastReceiver()    //提示信息接收器的定义   
  82.         {  
  83.             @Override  
  84.             public void onReceive(Context context, Intent intent) {  
  85.                 // TODO Auto-generated method stub   
  86.                 setText();      //收到intent,就及时修改TextView信息,使得Activity显示时,电池信息也能动态显示   
  87.             }  
  88.         };  
  89.           
  90.         /** 注册提示信息的intentFilter */  
  91.         IntentFilter filter=new IntentFilter();  
  92.         filter.addAction("com.ritterliu.newBatteryWidget");  
  93.         registerReceiver(infoReceiver,filter);  
  94.   
  95.         /** 启动提示service */  
  96.         Intent startService=new Intent(this,updateService.class);  
  97.         startService(startService);  
  98.           
  99.     }  
  100.       
  101.     /** 点击屏幕任意位置,关闭电池信息Activity */  
  102.     public boolean onTouchEvent(MotionEvent event)  
  103.     {  
  104.         this.finish();  
  105.     //  onDestroy();   
  106.     //  System.exit(0);   
  107.         return true;  
  108.     }  
  109.   
  110.     @Override  
  111.     protected void onDestroy() {  
  112.         // TODO Auto-generated method stub   
  113.         flag=false;  
  114.         unregisterReceiver(infoReceiver);  
  115.         super.onDestroy();  
  116.     }  
  117.   
  118.     /** 及时动态修改Activity上文字信息的函数 */  
  119.     public static  void setText()  
  120.     {  
  121.         String plugState="";  
  122.         switch(currentBatteryPlugged)  
  123.         {  
  124.         case 0:  
  125.             plugState="";  
  126.             break;  
  127.         case 1:  
  128.             plugState=batteryPluggedAC;  
  129.             break;  
  130.         case 2:  
  131.             plugState=batteryPluggedUSB;  
  132.             break;  
  133.         default:  
  134.             plugState="";  
  135.         }  
  136.   
  137.         switch(currentBatteryStatus)  
  138.         {  
  139.         case 1:  
  140.             tvBatteryStatus.setText(batteryStatus+batteryStatusUnknown);  
  141.             break;  
  142.         case 2:  
  143.             tvBatteryStatus.setText(batteryStatus+batteryStatusCharging+plugState);  
  144.             break;  
  145.         case 3:  
  146.             tvBatteryStatus.setText(batteryStatus+batteryStatusDischarging);  
  147.             break;  
  148.         case 4:  
  149.             tvBatteryStatus.setText(batteryStatus+batteryStatusNotCharging);  
  150.             break;  
  151.         case 5:  
  152.             tvBatteryStatus.setText(batteryStatus+batteryStatusFull+plugState);  
  153.             break;  
  154.         default:  
  155.             tvBatteryStatus.setText(batteryStatus+batteryStatusUnknown);  
  156.         }  
  157.           
  158.         tvBatteryLevel.setText(batteryLevel+String.valueOf(currentBatteryLevel)+"%");  
  159.           
  160.         switch(currentBatteryHealth)  
  161.         {  
  162.         case 1:  
  163.             tvBatteryHealth.setText(batteryHealth+batteryHealthUnknown);  
  164.             break;  
  165.         case 2:  
  166.             tvBatteryHealth.setText(batteryHealth+batteryHealthGood);  
  167.             break;  
  168.         case 3:  
  169.             tvBatteryHealth.setText(batteryHealth+batteryHealthOverheat);  
  170.             break;  
  171.         case 4:  
  172.             tvBatteryHealth.setText(batteryHealth+batteryHealthDead);  
  173.             break;  
  174.         case 5:  
  175.             tvBatteryHealth.setText(batteryHealth+batteryHealthOverVoltage);  
  176.             break;  
  177.         case 6:  
  178.             tvBatteryHealth.setText(batteryHealth+batteryHealthUnspecifiedFailure);  
  179.             break;  
  180.         case 7:  
  181.             tvBatteryHealth.setText(batteryHealth+batteryHealthCold);  
  182.             break;  
  183.         default:  
  184.             tvBatteryHealth.setText(batteryHealth+batteryHealthUnknown);  
  185.         }  
  186.           
  187.         tvBatteryTemperature.setText(batteryTemperature+currentBatteryTemperature/10f+"℃");  
  188.         tvBatteryVoltage.setText(batteryVoltage+currentBatteryVoltage+"mv");  
  189.         tvBatteryTechnology.setText(batteryTechnology+currentBatteryTechnology);  
  190.     }  
  191.       
  192.     /** 提示信息变化的service,约每隔一秒,就发送intent, 
  193.      * 提醒activity更新电池信息,主要为了检测电池状态的变化, 
  194.      * 例如连上充电时,状态会从“未在充电”变为“正在充电” 
  195.      * 通过调用plugged方式,还能判断是AC方式充电还是USB方式充电 
  196.      */  
  197.     public static class updateService extends Service{  
  198.         @Override  
  199.         public IBinder onBind(Intent intent) {  
  200.             // TODO Auto-generated method stub   
  201.             return null;  
  202.         }  
  203.           
  204.         /** 定义得到电池信息的BroadcastReceiver,提取出关键信息,存入变量中 */  
  205.         private BroadcastReceiver batteryReceiver=new BroadcastReceiver()  
  206.         {  
  207.             @Override  
  208.             public void onReceive(Context context, Intent intent) {  
  209.                 // TODO Auto-generated method stub   
  210.                 currentBatteryStatus=intent.getIntExtra("status", 0);  
  211.                 currentBatteryLevel=intent.getIntExtra("level", 0);  
  212.                 currentBatteryHealth=intent.getIntExtra("health", 0);  
  213.                 currentBatteryTemperature=intent.getIntExtra("temperature",0);  
  214.                 currentBatteryVoltage=intent.getIntExtra("voltage",0);  
  215.                 currentBatteryTechnology=intent.getStringExtra("technology");  
  216.                 currentBatteryPlugged=intent.getIntExtra("plugged",0);  
  217.             }  
  218.         };  
  219.           
  220.           
  221.         public void onStart(Intent intent,int startId)  
  222.         {  
  223.             registerReceiver(batteryReceiver,new IntentFilter(Intent.ACTION_BATTERY_CHANGED));//注册BroadcastReceiver   
  224.           
  225.             /** 启动一个线程,约每隔一秒就发送intent提醒Activity更新电池信息 */  
  226.             new Thread()  
  227.             {  
  228.                 public void run()  
  229.                 {  
  230.                     while(flag)  
  231.                     {  
  232.                         Intent sendInfoToActivity=new Intent();  
  233.                         sendInfoToActivity.setAction("com.ritterliu.newBatteryWidget");  
  234.                         sendBroadcast(sendInfoToActivity);  
  235.           
  236.                         try  
  237.                         {  
  238.                             Thread.sleep(1000);  
  239.                         }  
  240.                         catch(Exception ex)  
  241.                         {  
  242.                             ex.printStackTrace();  
  243.                         }  
  244.                     }  
  245.                 }  
  246.             }.start();  
  247.             super.onStart(intent, startId);  
  248.         }  
  249.     }  
  250. }  
linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款