I don't know, what I'm doing wrong. After launching my widget, I can see the battery status, but it doesn't refresh itself at all when I manually give telnet the "power capacity xx" command.
MainActivity.java
public class MainActivity extends AppWidgetProvider
{
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.activity_main);
Intent received = context.getApplicationContext()
.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
String receivedAction = received.getAction();
if (receivedAction.equals(Intent.ACTION_BATTERY_CHANGED))
{
int level = received.getIntExtra("level", 0);
views.setTextViewText(R.id.TextView1, level + "%!");
ComponentName appComponent = new ComponentName(context, MainActivity.class);
appWidgetManager.getInstance(context).updateAppWidget(appComponent, views);
}
}
}
widgetxml.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="72dp"
android:minHeight="72dp"
android:updatePeriodMillis="1800000"
android:initialLayout="@layout/activity_main"
>
</appwidget-provider>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/TextView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.widgetownia"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:label="@string/app_name"
android:icon="@drawable/ic_launcher" >
<receiver android:name=".MainActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"> </action>
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widgetxml" > </meta-data>
</receiver>
</application>
</manifest>