Saturday, 23 October 2010

Home Screen Battery Widget

It's a simple Battery Widget, with battery level and charging status displayed on Android Home Screen.

Home Screen Battery Widget

Create a layout file of our widget, simple with a TextView only - /res/layout/androidbatterywidget_layout.xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/level"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#000000"
/>
</LinearLayout>


The provider info, /res/xml/androidbatterywidgetproviderinfo.xml.
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="146dp"
android:minHeight="72dp"
android:initialLayout="@layout/androidbatterywidget_layout"
>
</appwidget-provider>


Implement AndroidBatteryWidgetProvider.java, it's our AppWidgetProvider.
It show "waiting" initially, and start our Service in onUpdate() method.
package com.exercise.AndroidBatteryWidget;

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 AndroidBatteryWidgetProvider extends AppWidgetProvider {


@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
updateWidget(context);
context.startService(new Intent(context, MyBatteryReceiver.class));
}

public void updateWidget(Context context){
RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.androidbatterywidget_layout);
updateViews.setTextViewText(R.id.level, "waiting!");

ComponentName myComponentName = new ComponentName(context, AndroidBatteryWidgetProvider.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(myComponentName, updateViews);
}

}


Implement MyBatteryReceiver.java.
In onCreate(), it register the BroadcastReceiver, myReceiver, to receive the event of Intent.ACTION_BATTERY_CHANGED. And also update widget in myReceiver.onReceive().
package com.exercise.AndroidBatteryWidget;

import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.IBinder;
import android.widget.RemoteViews;

public class MyBatteryReceiver extends Service {

private int batterylevel = 0;
private String batteryStatus ="";

private BroadcastReceiver myReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if (action.equals(Intent.ACTION_BATTERY_CHANGED))
{
batterylevel = intent.getIntExtra("level", 0);

int status = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);
String strStatus;
if (status == BatteryManager.BATTERY_STATUS_CHARGING){
batteryStatus = "Charging";
} else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING){
batteryStatus = "Dis-charging";
} else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING){
batteryStatus = "Not charging";
} else if (status == BatteryManager.BATTERY_STATUS_FULL){
batteryStatus = "Full";
} else {
batteryStatus = "";
}

updateAppWidget(context);
}
}

public void updateAppWidget(Context context){
RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.androidbatterywidget_layout);
updateViews.setTextViewText(R.id.level,
"Bat. Status:\n" +
"Level: " + batterylevel + "%\n" +
"Status: " + batteryStatus);

ComponentName myComponentName = new ComponentName(context, AndroidBatteryWidgetProvider.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(myComponentName, updateViews);
}
};

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(myReceiver, intentFilter);
}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
unregisterReceiver(myReceiver);
}

}


Modify AndroidManifest.xml to include receiver and server in our application.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exercise.AndroidBatteryWidget"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
<receiver android:name=".AndroidBatteryWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/androidbatterywidgetproviderinfo" />
</receiver>

<service android:name=".MyBatteryReceiver" android:label="MyBatteryReceiver">
<intent-filter>
<action android:name="android.intent.action.BATTERY_CHANGED" />
</intent-filter>
</service>

</application>
<uses-sdk android:minSdkVersion="4" />
</manifest>


Download the files.

Related Article:
- A simple Home Screen App Widget to get Date/Time.
- android.app.Service
- Detect Battery Info.



No comments:

Post a Comment