AppWidget3

Xml代码   收藏代码
  1. <uses-permission android:name="android.permission.BATTERY_STATS"></uses-permission>  
 

 

Java代码   收藏代码
  1. public class BatteryWidget extends AppWidgetProvider {  
  2.   
  3.     public void onUpdate(Context context, AppWidgetManager appWidgetManager,  
  4.             int[] appWidgetIds) {  
  5.         super.onUpdate(context, appWidgetManager, appWidgetIds);  
  6.         Intent intent = new Intent(context, AppBatteryService.class);  
  7.         context.startService(intent);// 不是context.startActivity(intent);  
  8.     }  
  9.   
  10.     /* UpdateService系统服务后台运行 */  
  11.     public static class AppBatteryService extends Service {//不要忘记static 否则会抛出Unable to instantiate service异常  
  12.         private UpdateReceiver receiver;  
  13.   
  14.         public IBinder onBind(Intent arg0) {  
  15.             return null;  
  16.         }  
  17.   
  18.         public void onStart(Intent intent, int startId) {  
  19.             super.onStart(intent, startId);  
  20.   
  21.             IntentFilter filter = new IntentFilter(  
  22.                     Intent.ACTION_BATTERY_CHANGED);  
  23.             receiver = new UpdateReceiver();  
  24.             registerReceiver(receiver, filter);  
  25.   
  26.             RemoteViews updateViews = getRemoteViews(this);  
  27.             /* 建立ComponentName对象与AppWidgetManager对象 */  
  28.             ComponentName thisWidget = new ComponentName(this,  
  29.                     BatteryWidget.class);  
  30.             AppWidgetManager manager = AppWidgetManager.getInstance(this);  
  31.             manager.updateAppWidget(thisWidget, updateViews);  
  32.         }  
  33.   
  34.         public void onDestroy() {  
  35.             unregisterReceiver(receiver);  
  36.             super.onDestroy();  
  37.         }  
  38.   
  39.         public RemoteViews getRemoteViews(Context context) {  
  40.             RemoteViews retRemoteViews = null;  
  41.             retRemoteViews = new RemoteViews(context.getPackageName(),  
  42.                     R.layout.update_widget);  
  43.             /* 取得保存在SharedPreferences的电量值 */  
  44.             int power = 0;  
  45.             SharedPreferences pres = context.getSharedPreferences("battery",  
  46.                     Context.MODE_PRIVATE);  
  47.             if (pres != null) {  
  48.                 power = pres.getInt("power"0);  
  49.             }  
  50.             /* 更新电量文字显示 */  
  51.             retRemoteViews.setTextViewText(R.id.myTextView1, power + "%");  
  52.             /* 更新电量图形显示 */  
  53.             if (power != 0) {  
  54.                 Bitmap bmp = BitmapFactory.decodeResource(  
  55.                         context.getResources(), R.drawable.power);  
  56.   
  57.                 float x = 57.0f / 100 * power;  
  58.                 Matrix matrix = new Matrix();  
  59.                 matrix.postScale(x, 1.0f);  
  60.                 Bitmap resizeBmp = Bitmap.createBitmap(bmp, 00139,  
  61.                         matrix, true);  
  62.   
  63.                 retRemoteViews.setBitmap(R.id.myImageView1,  
  64.   
  65.                 "setImageBitmap", resizeBmp);  
  66.             }  
  67.             return retRemoteViews;  
  68.         }  
  69.   
  70.         public class UpdateReceiver extends BroadcastReceiver {  
  71.   
  72.             public void onReceive(Context context, Intent intent) {  
  73.   
  74.                 if (intent.getAction().equals  
  75.   
  76.                 (Intent.ACTION_BATTERY_CHANGED)) {  
  77.   
  78.                     int intLevel = intent.getIntExtra("level"0);  
  79.                     int intScale = intent.getIntExtra("scale"100);  
  80.                     /* 将电量保存在SharedPreferences中 */  
  81.                     SharedPreferences pres =  
  82.   
  83.                     context.getSharedPreferences("battery",  
  84.                             Context.MODE_PRIVATE);  
  85.                     if (pres != null) {  
  86.                         SharedPreferences.Editor ed = pres.edit();  
  87.                         ed.putInt("power", (intLevel * 100 /  
  88.   
  89.                         intScale));  
  90.                         ed.commit();  
  91.                     }  
  92.                 }  
  93.             }  
  94.   
  95.         }  
  96.     }  
  97.   
  98. }  

 manifest.xml

 

Xml代码   收藏代码
  1. <receiver  
  2.             android:name=".BatteryWidget"  
  3.             android:label="@string/app_name">  
  4.             <intent-filter>  
  5.                 <action  
  6.                     android:name="android.appwidget.action.APPWIDGET_UPDATE" />  
  7.             </intent-filter>  
  8.             <meta-data  
  9.                 android:name="android.appwidget.provider"  
  10.                 android:resource="@xml/widget_template" />  
  11.         </receiver>  
  12.         <service  
  13.             android:name=".BatteryWidget$AppBatteryService" />  

 widget_template.xml

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <appwidget-provider  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:minWidth="72dip"   
  5.   android:minHeight="72dip"   
  6.   android:updatePeriodMillis="10000"   
  7.   android:initialLayout="@layout/update_widget"   
  8. />  

 update_widget.xml

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:background="@drawable/batt"  
  5.     android:orientation="vertical"  
  6.     android:layout_width="72dip"  
  7.     android:layout_height="72dip">  
  8.     <ImageView  
  9.         android:id="@+id/myImageView1"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_gravity="center"  
  13.         android:layout_marginLeft="5dip"  
  14.         android:layout_marginTop="6dip" />  
  15.     <TextView  
  16.         android:id="@+id/myTextView1"  
  17.         android:layout_width="fill_parent"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_x="24px"  
  20.         android:layout_y="22px"  
  21.         android:layout_marginTop="44dip"  
  22.         android:gravity="center_horizontal" />  
  23. </RelativeLayout>  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值