您当前的位置:首页 > 手机教程 > 安卓开发

Android Widget 电池插件的开发实现

时间:2014-04-15 13:45:29  来源:  作者:

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


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

 Android Widget 电池插件的开发实现 三联教程

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

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


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

下面介绍代码的实现:

整个工程主要实现两个部分,一个是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. }  


对于Widget,配置它的显示layout,一个简单的RelativeLayout布局,一个ImageView和一个TextView,居中显示:
 

  1. <span style="font-size:13px;"><?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></span>  

接着就是编写配置Widget的xml了,设置Widget大小等信息,在res目录下新建一个xml文件夹用来存放Widget的配置xml文件://备注2

  1. <span style="font-size:13px;"><?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></span>  


备注1,备注2:

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

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

  1. <span style="font-size:13px;">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. }  
  251. </span>  

布局文件:

  1. <span style="font-size:13px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/tvBatteryStatus"  
  9.         android:layout_marginLeft="3sp"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"   
  12.         android:text="@string/batteryStatus"  
  13.         android:textSize="18dp"  
  14.         android:textColor="#FFFFFF"  
  15.         />  
  16.       
  17.     <TextView  
  18.         android:id="@+id/tvBatteryLevel"  
  19.         android:layout_marginLeft="3sp"  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"   
  22.         android:text="@string/batteryLevel"  
  23.         android:textSize="18dp"  
  24.         android:textColor="#FFFFFF"  
  25.         />  
  26.       
  27.         <TextView  
  28.         android:id="@+id/tvBatteryHealth"  
  29.         android:layout_marginLeft="3sp"  
  30.         android:layout_width="wrap_content"  
  31.         android:layout_height="wrap_content"   
  32.         android:text="@string/batteryHealth"  
  33.         android:textSize="18dp"  
  34.         android:textColor="#FFFFFF"  
  35.         />  
  36.           
  37.         <TextView  
  38.         android:id="@+id/tvBatteryTemperature"  
  39.         android:layout_marginLeft="3sp"  
  40.         android:layout_width="wrap_content"  
  41.         android:layout_height="wrap_content"   
  42.         android:text="@string/batteryTemperature"  
  43.         android:textSize="18dp"  
  44.         android:textColor="#FFFFFF"  
  45.         />  
  46.               
  47.         <TextView  
  48.         android:id="@+id/tvBatteryVoltage"  
  49.         android:layout_marginLeft="3sp"  
  50.         android:layout_width="wrap_content"  
  51.         android:layout_height="wrap_content"   
  52.         android:text="@string/batteryVoltage"  
  53.         android:textSize="18dp"  
  54.         android:textColor="#FFFFFF"  
  55.         />  
  56.   
  57.         <TextView  
  58.         android:id="@+id/tvBatteryTechnology"  
  59.         android:layout_marginLeft="3sp"  
  60.         android:layout_width="wrap_content"  
  61.         android:layout_height="wrap_content"   
  62.         android:text="@string/batteryTechnology"  
  63.         android:textSize="18dp"  
  64.         android:textColor="#FFFFFF"  
  65.         />  
  66.   
  67.         <TextView  
  68.         android:id="@+id/tvInfo"  
  69.         android:layout_marginLeft="3sp"  
  70.         android:layout_width="wrap_content"  
  71.         android:layout_height="wrap_content"   
  72.         android:text="http://blog.csdn.net/ritterliu"  
  73.         android:textSize="15dp"  
  74.         android:textColor="#FFFFFF"  
  75.         />  
  76.   
  77. </LinearLayout></span>  

在代码中写了注释,还有什么不清楚的部分可以留言。

最后是AndroidManifest.xml:

  1. <span style="font-size:13px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.ritterliu.newBatteryWidget"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk android:minSdkVersion="4" />  
  8.   
  9.     <application  
  10.         android:icon="@drawable/j"  
  11.         android:label="@string/app_name" >  
  12.         <receiver  
  13.             android:label="@string/app_name"  
  14.             android:name=".NewBatteryWidget" >  
  15.             <intent-filter >  
  16.                 <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />  
  17.             </intent-filter>  
  18.             <meta-data android:name="android.appwidget.provider"  
  19.                 android:resource="@xml/new_battery_widget"  
  20.                 />  
  21.         </receiver>  
  22.          
  23.         <service android:name=".NewBatteryWidget$updateService"/>  
  24.               
  25.         <activity android:name=".NewBatteryInfoActivity" android:label="OtherActiviy_app_name"  
  26.             android:theme="@android:style/Theme.Dialog">  
  27.             <!-- android:theme="@android:style/Theme.Dialog" 这是设置Activity的主题风格为对话框形式 -->  
  28.         </activity>  
  29.           
  30.         <service android:name=".NewBatteryInfoActivity$updateService"/>  
  31.           
  32.     </application>  
  33.   
  34. </manifest></span>  


大功告成

总结:

本次开发大概前后折腾了4天时间,这其中绕了一段弯路(就是那个android:updatePeriodMillis), 不仅学习了如何开发Widget,还对Activity的一些写法,比如设置风格为Dialog等又有了进一步的学习,在关闭Activity时,我是直 接调用的finish关闭的,也许在用法上还有不当之处,本人从9月份开始自学Android开发至今两个多月的时间,开发水平还十分有限,代码中有写的 不好的地方还请大家多多指点,不甚感激。

附上完整文件下载地址,由于本人积分很少(只有19 ),故略收1分,一周后将改为免费,谢谢大家。

http://download.csdn.net/detail/ritterliu/3791897

 

©copyrights all reserved 2004-2019 严禁拷贝版面设计,违者必究!
电脑学习网版权所有 侵权必究 Powered by 百年互动