0

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>
polandsux
  • 5
  • 1
  • 7

1 Answers1

0

As you are using AppWidgetProvider you can update your widget either from any activity or in your case you can update it from service. Check below links to know how to refresh widget.

Programmatically update widget from activity/service/receiver

Force Android widget to update

Also check wonderful tutorial provided by vogella explaining everything about widget. It also shows how to update widget via click on it or from any service.

http://www.vogella.com/tutorials/AndroidWidgets/article.html

Community
  • 1
  • 1
baldguy
  • 2,090
  • 1
  • 16
  • 25