1

I'm trying to write an Android widget that updates itself to different messages in text[]. It outputs the first message "Test1", but does not proceed. It updates when I select a new widget to place on home screen. It doesn't spit anything out in the logcat. It's supposed to update the widget every 5 seconds. I chose 5 seconds just for testing purpose.

Manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.messageswidget"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <receiver android:name="main.MessagesWidget" 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/widgetinfo" />
    </receiver>

</application>

MessagesWidget.java

package main;


import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.util.Log;
import android.widget.RemoteViews;

import com.example.messageswidget.R;

public class MessagesWidget extends AppWidgetProvider {
    private static int n = 0;
    String[] text = {"Test1", "More2", "Good3"};

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        Log.d("test", "good");
        // TODO Auto-generated method stub
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
        views.setTextViewText(R.id.msg, text[n]);
        n = (n + 1) % text.length;

        ComponentName cname = new ComponentName(context, MessagesWidget.class);
        appWidgetManager.updateAppWidget(cname, views);
    }
}

widgetlayout.xml

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

<TextView android:id="@+id/msg"
    android:textIsSelectable="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>

widgetinfo.xml

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

</appwidget-provider>
awonderer
  • 665
  • 9
  • 26

2 Answers2

1

You can't update widgets every 5 seconds (not the way you are doing it at least) android:updatePeriodMillis is limited to 30 minutes.

Ahmad
  • 69,608
  • 17
  • 111
  • 137
  • Roughly speaking, would I need a service for that? Is the `updatePeriodMillis` only implicit and not supposed to automatically execute onUpdate method for me? – awonderer Mar 27 '13 at 18:50
  • updatePeriodMillis is limited to 30000 ms (30 minutes). You can use an Alarmmanager to schedule more frequent updates. – Ahmad Mar 27 '13 at 18:53
  • I am using a JSON parser to retrieve the information to show it in `TextView` but it's not working. Can you help? http://stackoverflow.com/questions/20100117/widget-is-not-updating-after-json-parse – Si8 Nov 20 '13 at 16:02
1

I had the similar problem today. It has gone after a sort of black magic ritual.

Have no idea what it was and what to do when the problem appears next time.

Community
  • 1
  • 1
pell
  • 33
  • 1
  • 8