Brandon
Brandon

Reputation: 703

Widget Not Updating?

This is only my second time using widgets, and first time using buttons on it... or anything other than just TextViews in general.

So basically all I have is a widget... When I click it it doesn't do anything and it never updates. Why? Confused... :P

So, here is my WidgetAct onUpdate code:

@Override
public void onUpdate(Context c, AppWidgetManager awm, int[] appWidgetIds){
    Log.i("IN","onUpdate");
    RemoteViews rv = new RemoteViews(c.getPackageName(),R.layout.widget);

    Intent twitter_intent = new Intent(c,TwitterUpdate.class);
    twitter_intent.setAction(ACTION_WIDGET_TWITTER);

    Intent fb_intent = new Intent(c,FBUpdate.class);
    fb_intent.setAction(ACTION_WIDGET_FB);

    //Intent curr = new Intent(c,WidgetAct.class);
    //curr.setAction(ACTION_WIDGET_WA);

    PendingIntent twitter_pi = PendingIntent.getActivity(c, 0, twitter_intent, 0);
    PendingIntent fb_pi = PendingIntent.getActivity(c,0,fb_intent,0);

    rv.setOnClickPendingIntent(R.id.tweet, twitter_pi);
    rv.setOnClickPendingIntent(R.id.fb, fb_pi);

    awm.updateAppWidget(appWidgetIds, rv);
}

Now here is my manifest (or the important part):

 <receiver android:name="WidgetAct">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            <action android:name="com.laytproducts.IN.WidgetAct.ACTION_WIDGET_FB" />
            <action android:name="com.laytproducts.IN.WidgetAct.ACTION_WIDGET_TWITTER" />
        </intent-filter>
        <meta-data android:name="android.appwidget.provider"
            android:resource="@xml/winfo"/>
    </receiver>

Here is my xml for the widget provider:

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth = "220dp"
android:minHeight = "72dp"
android:updatePeriodMillis="0"
android:initialLayout="@layout/widget">

Here is my layout:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/linearLayout1" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical">
<FrameLayout android:background="@drawable/inwidget" android:id="@+id/frameLayout1" android:layout_width="wrap_content" android:layout_height="wrap_content">
    <RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent">
        <ImageButton android:scaleType="fitXY" android:src="@drawable/twitterlogo" android:layout_marginTop="17dp" android:id="@+id/tweet" android:layout_marginLeft="20dp" android:layout_width="40dp" android:layout_height="40dp"></ImageButton>
        <ImageButton android:scaleType="fitXY" android:src="@drawable/facebooklogo" android:layout_marginLeft="5dp" android:id="@+id/fb" android:layout_toRightOf="@+id/tweet" android:layout_alignTop="@+id/tweet" android:layout_alignBottom="@+id/tweet" android:layout_width="40dp" android:layout_height="40dp"></ImageButton>
    </RelativeLayout>

</FrameLayout>

Kind of a lot of code... but I literally have NO reason to believe that I broke something XD... but that is because my widget knowledge is minimal.

Hopefully its a small mistake I over looked. Thanks all, Brandon

Upvotes: 0

Views: 768

Answers (3)

Huang
Huang

Reputation: 4842

According to the explanation in the SDK document, the onUpdate() method is designed to respond to the ACTION_APPWIDGET_UPDATE broadcast, which will be sent in conditions below:

Sent when it is time to update your AppWidget. This may be sent in response to a new instance for this AppWidget provider having been instantiated, the requested update interval having lapsed, or the system booting.

In your code, you set the action com.laytproducts.IN.WidgetAct.ACTION_WIDGET_FB, so the onUpdate() won't be called. I haven't used getActivity in widget, but I have used getBroadcast and it works well.

PendingIntent fb_pi = PendingIntent.getBroadcast(c,0,fb_intent,0);

I think you should handle your click event in the onReceive() method. Please note that the AppWidgetProvider extends from BroadcastReceiver, so only onReceive() will be called(in the onReceive() in the source code of AppWidgetProvider, onUpdate() is called inside. That's why we have onUpdate()). You should do something like:

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
    String action = intent.getAction();

    if (action.equals("com.laytproducts.IN.WidgetAct.ACTION_WIDGET_FB")) {
           //handle your click event here

        }

}

This is what I did in my widget. Hope it helps.

Upvotes: 1

YshakPK
YshakPK

Reputation: 181

You can try this..

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
  RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
  Intent one = new Intent(context, TwitterUpdate.class);     
  Intent two = new Intent(context, FBUpdate.class);   
  PendingIntent p1 = PendingIntent.getActivity(context, 0, one, 0);
  PendingIntent p2 = PendingIntent.getActivity(context, 0, two, 0);
  remoteViews.setOnClickPendingIntent(R.id.twitter_pi, p1);
  remoteViews.setOnClickPendingIntent(R.id.fb_pi, p2);
  appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}

Upvotes: 0

YshakPK
YshakPK

Reputation: 181

If you want your widget to update, you should have to set the time interval in "updatePeriodMillis" field in the XML file .But remember ,the minimum time interval for updating using this method is 30 min.

Upvotes: 2

Related Questions