Note that there are some explanatory texts on larger screens.

plurals
  1. POWidget not updating
    primarykey
    data
    text
    <p>I'm trying to create a battery widget, but the textView that is supposed to show battery state isn't updating. It only changes if the phone is restarted. What is missing from the code?</p> <p><strong>AndroidManifest:</strong></p> <pre><code> &lt;uses-permission android:name="android.permission.BATTERY_STATS" /&gt; &lt;application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" &gt; &lt;activity android:name=".WidgetConfigurationActivity" android:excludeFromRecents="true" android:launchMode="singleTop" &gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;receiver android:name=".MAppWidgetProvider" &gt; &lt;intent-filter&gt; &lt;action android:name="android.appwidget.action.APPWIDGET_UPDATE" /&gt; &lt;/intent-filter&gt; &lt;meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_info" /&gt; &lt;/receiver&gt; &lt;/application&gt; </code></pre> <p></p> <p><strong>AppWidgetProvider:</strong></p> <pre><code> public class MAppWidgetProvider extends AppWidgetProvider { private String batteryLevel="init"; @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { context.getApplicationContext().registerReceiver(this, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); updateView(context); } @Override public void onReceive(Context context, Intent intent) { int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); int level = -1; level = (rawlevel * 100) / scale; batteryLevel = Integer.toString(level); updateView(context); super.onReceive(context, intent); } public void updateView(Context context) { RemoteViews thisViews = new RemoteViews(context.getApplicationContext() .getPackageName(), R.layout.widget_layout); thisViews.setTextViewText(R.id.textView1, batteryLevel); ComponentName thisWidget = new ComponentName(context, AnalogClockWidgetProvider.class); AppWidgetManager.getInstance(context).updateAppWidget(thisWidget, thisViews); } </code></pre> <p>}</p> <p><strong>Info:</strong></p> <pre><code> &lt;appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="40dp" android:minHeight="40dp" android:initialLayout="@layout/widget_layout" android:updatePeriodMillis="10000" android:configure="in.wptrafficanalyzer.appwidgetclockconfiguration.WidgetConfigurationActivity" android:resizeMode="horizontal|vertical"&gt; &lt;/appwidget-provider&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload