Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android AppWidget Not Displaying On Certain Phones

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();
    }

}
like image 987
Eliezer Avatar asked Nov 13 '22 08:11

Eliezer


1 Answers

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...

like image 132
ChD Computers Avatar answered Nov 16 '22 03:11

ChD Computers