Android Battery Widget

As a sample of Widget, Let’s make Easy Battery Widget.
Widget updates when changes levels.
First widget explanation is here

Steps

  1. Prepare main layout(xml)
  2. Make widget definition file(xml)
  3. Create Battery receiver class which extends BroadcastReceiver
  4. Create Service for BroadcastReceiver
  5. Create Main widget class(AppWidgetProvider) to start service
  6. Add configuration to AndroidManifest.xml

res/layout/main.xml

This is layout file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <TextView android:id="@+id/battery"
		android:layout_width="fill_parent"
		android:layout_height="match_parent"
		android:textSize="30sp"
		android:textAlignment="center"
		android:gravity="center"
		android:text="Boo"
	/>
</LinearLayout>

res/xml/mainwidget.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="0"
		android:initialLayout="@layout/main" />

The key point is updatePeriodMillis.
I set this parameter 0, means never update.
The parameter updates when

BatteryReciver

This is broadcast receiver to get battery level using service.

public class BatteryReciver extends BroadcastReceiver 
{
	private static final String LEVEL = "level"; 
	
	private static final String SCALE = "scale"; 
	
	private int scale = 0 ;
	
	private int level = 0;
	
	@Override
	public void onReceive(Context context, Intent intent) 
	{
		String action = intent.getAction();
		
		if ( action.equals(Intent.ACTION_BATTERY_CHANGED) ) 
		{
			level = intent.getIntExtra(LEVEL, 0);
			scale = intent.getIntExtra(SCALE, 0);
		}
		
		AppWidgetManager awm = AppWidgetManager.getInstance(context);
		ComponentName cn = new ComponentName(context, MainWidget.class);
        RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.main);
        rv.setTextViewText(R.id.battery, ""+(int)(level*100/scale));
        awm.updateAppWidget(cn, rv);
	}
}

Service

public class WidgetService extends Service 
{
	static BatteryReciver reciver = new BatteryReciver();
	
	@Override
	public void onStart(Intent intent, int startId) 
	{
		IntentFilter filter = new IntentFilter();
		filter.addAction(Intent.ACTION_BATTERY_CHANGED);
		registerReceiver(reciver, filter);
	}

	@Override
	public IBinder onBind(Intent intent) 
	{
		return null;
	}
}

Widget

public class WidgetService extends Service 
{
	static BatteryReciver reciver = new BatteryReciver();
	
	@Override
	public void onStart(Intent intent, int startId) 
	{
		IntentFilter filter = new IntentFilter();
		filter.addAction(Intent.ACTION_BATTERY_CHANGED);
		registerReceiver(reciver, filter);
	}

	@Override
	public IBinder onBind(Intent intent) 
	{
		return null;
	}
}

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.atmarkplant.battery"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        
        <receiver android:name="com.atmarkplant.battery.MainWidget" android:label="Battery">
			<intent-filter>
				<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
			</intent-filter>
			<meta-data android:name="android.appwidget.provider" android:resource="@xml/mainwidget" />
		</receiver>
        <service android:name="com.atamrkplant.battery.service.WidgetService"></service>
    </application>
</manifest>