Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android widget update period milles not working

I am a new android developer trying to code a widget.. The "updatePeriodMillis" parameter tell the widget to call onupdate method after the given duraion but it doesn't happen so for me....

can some one please point my mistake.. here are my files

src/hellowidget.java

package de.thesmile.android.widget;

import android.appwidget.AppWidgetProvider;
import java.util.Random;

import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;



public class HelloWidget extends AppWidgetProvider {
    int number =0;


    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {

         number = (new Random().nextInt(100));
        for (int appWidgetId : appWidgetIds) {


            RemoteViews views = new RemoteViews(context.getPackageName(),R.layout.main);
            views.setTextViewText(R.id.TextView01, String.valueOf(number));
            appWidgetManager.updateAppWidget(appWidgetId, views);

        }

    }


}

xml/hello_widget_provider.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="146dip"
android:minHeight="72dip"
android:updatePeriodMillis="1000"
android:initialLayout="@layout/main"
/>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent"
android:orientation="vertical"

android:layout_gravity="center"
android:layout_height="wrap_content">
<TextView android:text="@+id/TextView01" android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView><TextView android:padding="10dip" android:layout_marginTop="5dip" android:layout_gravity="center_horizontal|center" android:text="@string/widget_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/widget_textview" android:textColor="@android:color/black"></TextView>


</LinearLayout>

android manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.thesmile.android.widget"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<!-- Broadcast Receiver that will process AppWidget updates -->
<receiver android:name=".HelloWidget" android:label="@string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/hello_widget_provider" />
</receiver>
</application>
<uses-sdk android:minSdkVersion="7" />
</manifest>
like image 656
Gaurav Shah Avatar asked Mar 05 '11 16:03

Gaurav Shah


2 Answers

The update time for widgets is forced to at least 30mins, to avoid poor programmed widgets to dry battery. Use AlarmManager to overcome this (see this post).

like image 150
bigstones Avatar answered Oct 19 '22 17:10

bigstones


The updatePeriodMillis attribute defines how often the App Widget framework should request an update from the AppWidgetProvider by calling the onUpdate() callback method. The actual update is not guaranteed to occur exactly on time with this value and we suggest updating as infrequently as possible—perhaps no more than once an hour to conserve the battery.

If, you need to update more frequently (i.e 1 second in your case, which is not at all recommended). To do so, set an alarm with an Intent that your AppWidgetProvider receives, using the AlarmManager. Set the alarm type to either ELAPSED_REALTIME or RTC, which will only deliver the alarm when the device is awake. Then set updatePeriodMillis to zero ("0").

like image 24
parvez rafi Avatar answered Oct 19 '22 17:10

parvez rafi