android Widget-显示电量

BatteyAppWidget.java


package widget.irdc;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

public class BatteyAppWidget extends AppWidgetProvider {
 public final static String TAG = "BatteyAppWidgetProvider";
 public static BatteyAppWidget mInstance;
 
 public BatteyAppWidget getInstance() {
  if (mInstance == null) {
   mInstance = new BatteyAppWidget();
  }
  return mInstance;
 }

 public void onUpdate(Context context, AppWidgetManager appwidgetmanager,
   int[] appwidgetIds) {
  Intent intent = new Intent(context, BatteyService.class);
  context.startService(intent);
  RemoteViews views = new RemoteViews(context.getPackageName(),
    R.layout.main);
  appwidgetmanager.updateAppWidget(appwidgetIds, views);
 }

 public void onDisabled(Context context) {
  super.onDisabled(context);
  Intent intent = new Intent(context, BatteyService.class);
  context.stopService(intent);
 }

 public static void notifyChange(Context context, int m) {
  ComponentName thiswidget = new ComponentName(context,
    BatteyAppWidget.class);
  AppWidgetManager appmanager = AppWidgetManager.getInstance(context);
  RemoteViews Views = new RemoteViews(context.getPackageName(),
    R.layout.main);
  Views.setTextViewText(R.id.tips, m + "%");
  if (m > 0 && m <= 25) {
   Views.setInt(R.id.ll, "setBackgroundResource", R.drawable.i25);
  }
  if (m > 25 && m <= 50) {
   Views.setInt(R.id.ll, "setBackgroundResource", R.drawable.i50);
  }
  if (m > 50 && m <= 75) {
   Views.setInt(R.id.ll, "setBackgroundResource", R.drawable.i75);
  }
  if (m > 75 && m <= 100) {
   Views.setInt(R.id.ll, "setBackgroundResource", R.drawable.i100);
  }
  appmanager.updateAppWidget(thiswidget, Views);
 }
}
 
BatteyService.java

package widget.irdc;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
public class BatteyService extends Service {
 
//必须使用广播来得到电量的值
 public BroadcastReceiver battryreceiver=new BroadcastReceiver()
 {
  @Override
  public void onReceive(Context context, Intent intent) {
   // TODO Auto-generated method stub
   String action=intent.getAction();
   if(action.equals(Intent.ACTION_BATTERY_CHANGED))
   {
    int level=intent.getIntExtra("level",0);
    int scale=intent.getIntExtra("scale",100);
    int lenvel=level*100/scale;
    BatteyAppWidget.notifyChange(context,lenvel);
   }
  }
 };
 @Override
 public IBinder onBind(Intent arg0) {
  // TODO Auto-generated method stub
  return null;
 }
 
 public void onStart(Intent intent,int startId)
 {
        IntentFilter mIntentFilter=new IntentFilter();
        mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
        registerReceiver(battryreceiver,mIntentFilter);
 }
 
 public void onDestroy() {
  unregisterReceiver(battryreceiver);
  super.onDestroy();
 }
}
 
main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/ll"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/i0"
    android:orientation="vertical"
    android:layout_gravity="center" 
    >

<TextView android:id="@+id/tips"
    android:text="Battery level:" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal|center" 
 android:paddingTop="75px"
 android:paddingLeft="60px"
    android:textColor="@android:color/white"/>
</LinearLayout>
 
battry_widget.xml

<?xml version="1.0" encoding="utf-8" ?>
  <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 
   android:initialLayout="@layout/main" android:updatePeriodMillis="120000" 
   android:minHeight="50px" 
   android:minWidth="50px" />
 
<!--  android:updatePeriodMillis="86400000" 为插件刷新的时间,单位为毫秒,86400000毫秒=24小时
android:initialLayout="@layout/main" 为初始化的界面摆布情况
android:minHeight="67dip"  为插件最小高度
android:minWidth="122dip"  为插件最小宽度 -->
 

 

你可能感兴趣的:(android,xml,OS)