- <uses-permission android:name="android.permission.BATTERY_STATS"></uses-permission>
- public class BatteryWidget extends AppWidgetProvider {
- public void onUpdate(Context context, AppWidgetManager appWidgetManager,
- int[] appWidgetIds) {
- super.onUpdate(context, appWidgetManager, appWidgetIds);
- Intent intent = new Intent(context, AppBatteryService.class);
- context.startService(intent);// 不是context.startActivity(intent);
- }
- /* UpdateService系统服务后台运行 */
- public static class AppBatteryService extends Service {//不要忘记static 否则会抛出Unable to instantiate service异常
- private UpdateReceiver receiver;
- public IBinder onBind(Intent arg0) {
- return null;
- }
- public void onStart(Intent intent, int startId) {
- super.onStart(intent, startId);
- IntentFilter filter = new IntentFilter(
- Intent.ACTION_BATTERY_CHANGED);
- receiver = new UpdateReceiver();
- registerReceiver(receiver, filter);
- RemoteViews updateViews = getRemoteViews(this);
- /* 建立ComponentName对象与AppWidgetManager对象 */
- ComponentName thisWidget = new ComponentName(this,
- BatteryWidget.class);
- AppWidgetManager manager = AppWidgetManager.getInstance(this);
- manager.updateAppWidget(thisWidget, updateViews);
- }
- public void onDestroy() {
- unregisterReceiver(receiver);
- super.onDestroy();
- }
- public RemoteViews getRemoteViews(Context context) {
- RemoteViews retRemoteViews = null;
- retRemoteViews = new RemoteViews(context.getPackageName(),
- R.layout.update_widget);
- /* 取得保存在SharedPreferences的电量值 */
- int power = 0;
- SharedPreferences pres = context.getSharedPreferences("battery",
- Context.MODE_PRIVATE);
- if (pres != null) {
- power = pres.getInt("power", 0);
- }
- /* 更新电量文字显示 */
- retRemoteViews.setTextViewText(R.id.myTextView1, power + "%");
- /* 更新电量图形显示 */
- if (power != 0) {
- Bitmap bmp = BitmapFactory.decodeResource(
- context.getResources(), R.drawable.power);
- float x = 57.0f / 100 * power;
- Matrix matrix = new Matrix();
- matrix.postScale(x, 1.0f);
- Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0, 1, 39,
- matrix, true);
- retRemoteViews.setBitmap(R.id.myImageView1,
- "setImageBitmap", resizeBmp);
- }
- return retRemoteViews;
- }
- public class UpdateReceiver extends BroadcastReceiver {
- public void onReceive(Context context, Intent intent) {
- if (intent.getAction().equals
- (Intent.ACTION_BATTERY_CHANGED)) {
- int intLevel = intent.getIntExtra("level", 0);
- int intScale = intent.getIntExtra("scale", 100);
- /* 将电量保存在SharedPreferences中 */
- SharedPreferences pres =
- context.getSharedPreferences("battery",
- Context.MODE_PRIVATE);
- if (pres != null) {
- SharedPreferences.Editor ed = pres.edit();
- ed.putInt("power", (intLevel * 100 /
- intScale));
- ed.commit();
- }
- }
- }
- }
- }
- }
manifest.xml
- <receiver
- android:name=".BatteryWidget"
- android:label="@string/app_name">
- <intent-filter>
- <action
- android:name="android.appwidget.action.APPWIDGET_UPDATE" />
- </intent-filter>
- <meta-data
- android:name="android.appwidget.provider"
- android:resource="@xml/widget_template" />
- </receiver>
- <service
- android:name=".BatteryWidget$AppBatteryService" />
widget_template.xml
- <?xml version="1.0" encoding="utf-8"?>
- <appwidget-provider
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:minWidth="72dip"
- android:minHeight="72dip"
- android:updatePeriodMillis="10000"
- android:initialLayout="@layout/update_widget"
- />
update_widget.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:background="@drawable/batt"
- android:orientation="vertical"
- android:layout_width="72dip"
- android:layout_height="72dip">
- <ImageView
- android:id="@+id/myImageView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_marginLeft="5dip"
- android:layout_marginTop="6dip" />
- <TextView
- android:id="@+id/myTextView1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_x="24px"
- android:layout_y="22px"
- android:layout_marginTop="44dip"
- android:gravity="center_horizontal" />
- </RelativeLayout>