5

I have a really simple appwidget (two text views and a button). I've tested it on a Touchpad, Droid 1, and a Droid Razr. It works on everything except the Razr. When I add the widget to the homescreen it doesn't display; it's just invisible. If I hold down on the spot where it would be it selects a widget and if I move it around I see other widgets move out of the way but it's completely invisible.

I put some Toasts in the onReceive and onEnabled methods and the Toast displays all the right information (ie intent action and extras).

Anybody have any experience with this?

EDIT: Please keep in mind this is just for debugging and does not follow best practices

public class GoogleTalkWidget extends AppWidgetProvider {

    Button sendMessage;

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Received Intent Action = " +
                intent.getAction(), Toast.LENGTH_SHORT).show();

        if(intent.getAction().equals(Utils.RECEIVED_MESSAGE_WIDGET)){
            RemoteViews views = new RemoteViews(context.getPackageName(),
                R.layout.main_widget);

            views.setTextViewText(R.id.widget_message, 
                (CharSequence)intent.getStringExtra("MESSAGE"));

            views.setTextViewText(R.id.widget_sender, 
                (CharSequence)intent.getStringExtra("SENDER"));

            Toast.makeText(context, "Received " + 
                intent.getStringExtra("MESSAGE") + " FROM " + 
                intent.getStringExtra("SENDER"), Toast.LENGTH_SHORT).show();

            ComponentName cn = new ComponentName(context, 
                GoogleTalkWidget.class);  

            AppWidgetManager.getInstance(context).updateAppWidget(cn, views);

        }
        super.onReceive(context, intent);
    }

    @Override 
    public void onEnabled(final Context context){
        super.onEnabled(context);

        Toast.makeText(context, "Enabled", Toast.LENGTH_SHORT).show();
    }

}
Eliezer
  • 7,209
  • 12
  • 56
  • 103
  • `onReceive()`? Do you mean `onUpdate()` of your `AppWidgetProvider`? – CommonsWare Mar 30 '12 at 22:41
  • you can also override the `onReceive()` method in an appwidget...I think they left it in because earlier versions of Android had some issues with `onDelete` – Eliezer Mar 30 '12 at 22:42
  • I understand that. However, `onUpdate()` is generally where you would supply the `RemoteViews` to declare what should go in the app widget. Is `onUpdate()` being called and are you successfully supplying the `RemoteViews`? Is there anything in LogCat that might relate (e.g., stack traces)? – CommonsWare Mar 30 '12 at 22:53
  • Check the added code above....I don't want to use updates....I use this as a broadcast receiver and have my service send out a broadcast whenever I need an update....more of a push notification than a poll :-). In any event there are no stack traces or errors that I can see...in fact the code runs perfectly other than the fact that on the Razr the widget does not display – Eliezer Apr 01 '12 at 01:13
  • "I don't want to use updates" -- you do not have a choice, at least for your initial update. – CommonsWare Apr 01 '12 at 10:44
  • I don't know why you're so hung up on `onReceive`. The android docs clearly say that you are able to use an appwidget as a broadcast receiver by only implementing `onReceive` and besides, that's not the issue the appwidget works perfectly everywhere...the issue is that on certain phones it won't display....and yes I have tried removing `onReceive` just to be sure and nothing changed....can we move past that please – Eliezer Apr 01 '12 at 15:03

1 Answers1

1

The best thing I can think of (supposed there isn't a bug or other problem with the launcher of the Razr) is that your resources aren't configured correctly. Maybe the Razr has a different dpi density and there aren't resources for that density in your project.

Try for example to move all of the drawables that make up your widget to the folder res\drawable-nodpi and see how it's going.

EDIT: I saw something strange in your code

In your GoogleTalkWidget class onReceive method, your widget is only updated when the message Utils.RECEIVED_MESSAGE_WIDGET is received. I don't know what this message is for but an app-widget, the first time is added on the home screen, it receives the android.appwidget.action.APPWIDGET_UPDATE and any other intent filters are registered in the manifest file and they are broadcasted by the system at that time (and the sticky broadcasts intents of course).

If I was in your position I would change the onReceive method as follows:

@Override
public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "Received Intent Action = " +
            intent.getAction(), Toast.LENGTH_SHORT).show();
    super.onReceive(context, intent);

    String msg = "No messages yet";

    RemoteViews views = new RemoteViews(context.getPackageName(),
            R.layout.main_widget);

    if(intent.getAction().equals(Utils.RECEIVED_MESSAGE_WIDGET)){
        msg = intent.getStringExtra("MESSAGE");

        views.setTextViewText(R.id.widget_sender, 
            (CharSequence)intent.getStringExtra("SENDER"));

        Toast.makeText(context, "Received " + 
            intent.getStringExtra("MESSAGE") + " FROM " + 
            intent.getStringExtra("SENDER"), Toast.LENGTH_SHORT).show();

        ComponentName cn = new ComponentName(context, 
            GoogleTalkWidget.class);  
    }

    views.setTextViewText(R.id.widget_message, msg);

    AppWidgetManager.getInstance(context).updateAppWidget(cn, views);
}

and see what happens.

If your widget fails to appear then it could be a problem with Razr but this is unlike because I suppose that a whole bunch of other widgets works fine.

Furthermore, although your code is for debugging only, your approach is a little bit problematic. The best place to update your widget views is in the onUpdate method of the AppWidgetProvider and not in the onReceive. Android provides the onUpdate method for that purpose and the onReceive to inform you that a registered broadcast has arrived. The basic difference is that in onUpdate method, Android has extracted all the needed parameters for you from the received Intent extras. One more thing about widget updates is that you should provide an android:updatePeriodMillis value other than 0 (2100000 is a good value) in your widget xml file even if you don't want periodic updates for your widget. I saw somewhere that the onReceive method may not be called if this value is 0.

Keep in mind also that AppWidgetProvider as a Broadcast Receiver lives only as long as the onReceive method does its job, after that is is destroyed thus it is not a good place for "interactive" code like UI listeners etc. I am telling you this because you have a Button declaration (Button sendMessage;) in the top of your GoogleTalkWidget class.

Hope this helps...

ChD Computers
  • 3,135
  • 3
  • 23
  • 33
  • Right now I'm just trying to get it to work so its very bare bones...no drawables or anything like that....just a textview and button – Eliezer May 13 '12 at 01:54
  • As for the content of `onReceive` I haven't been updating this post since I've forgotten about it and moved on when I still could not solve the issue. Since I've posted the question I've tried taking parts of code out until I only had an `onUpdate` which called `super` and that was it; still didn't work. As for `onUpdate` vs `onReceive` I haven't really given it much thought since this project went on the backburner due to this issue with the Razr but your input is appreciated! – Eliezer May 13 '12 at 23:10