Hi guys,

I am trying to make an appwidget to be displayed when we click a
button.

I am having a simple application which has a button.

When the button is clicked,it will send a broadcast message.
I am implementing an appwidgetprovider class in the same application.

I have overridden the onReceive function in the appwidgetprovider
class as shown below:


        @Override
        public void onReceive ( Context context,Intent intent ) {

                String action = intent.getAction();
                String message="";
                //String packagename="";
                //int appwidgetid;
                //String RESULT_OK = "sucessfull";

                Log.i("test", "message received");

                if( "android.appwidget.action.APPWIDGET_ENABLED".equals ( 
action ) )
{

                        //String message;
                        Toast.makeText( context, "Broadcast sucessfull...",
Toast.LENGTH_LONG).show();

                        if ( intent.hasExtra(broadcast.MESSAGE)) {

                        message = 
(String)intent.getCharSequenceExtra(broadcast.MESSAGE);
                        Toast.makeText( context, message, 
Toast.LENGTH_LONG).show();

                        }



                        ComponentName component;
                        component = new ComponentName(context,receiver.class);


                        RemoteViews views = new 
RemoteViews(context.getPackageName
(),R.layout.widget_provider);
                        views.setTextViewText(R.id.message, message);
                    }
             }
}

I am able to see the toast messages but i am not able to see the
appwidget.

In the above code R.layout.widget_provider is the xml file which has
the appwidget-provider tag.
I have also mention this file as the resource file in the android
manifest file.

My questions are:
1.Is this code correct?
2.What should be the second parameter in the instantiation of
"Remoteviews" class?
3.What should be the first parameter in the setTextViewText function?

Please anybody help me on this...







On Nov 12, 6:45 am, Susan <ska...@gmail.com> wrote:
> You guys are my heros! I've been trying to overcome this single issue
> for about two hours. That's fantastic. It works beautifully. Thanks!
>
> For the sake of posterity (and anybody else who might be trying to
> solve this), here's my working code:
>
> (the lines are long so I apologize for the crummy formatting)
>
>      public class Widget extends AppWidgetProvider
>      {
>          public void onReceive(Context context, Intent intent)
>          {
>              String action = intent.getAction();
>              if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals
> (action))
>              {
>
>                  RemoteViews views = new RemoteViews(
>
> context.getPackageName(),R.layout.widget);
>
>                  Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse
> ("http://www.cnn.com";));
>                  i.addCategory(Intent.CATEGORY_BROWSABLE);
>                  i.setComponent(new ComponentName
> ("com.android.browser",
>
> "com.android.browser.BrowserActivity"));
>                  PendingIntent pendingIntent =
> PendingIntent.getActivity(context, 0, i, 0);
>                  views.setOnClickPendingIntent(R.id.Widget,
> pendingIntent);
>
>                  AppWidgetManager.getInstance(context).updateAppWidget
>                                               (intent.getIntArrayExtra
> (AppWidgetManager.
>                                                EXTRA_APPWIDGET_IDS),
> views);
>              }
>          }
>      }
>
> On Nov 11, 7:33 pm, Jason Proctor <jason.android.li...@gmail.com>
> wrote:
>
> > you can't instantiate a Uri from a string, need to use the parse()
> > factory method.
>
> > this is how i do it, works for me --
>
> > Intent  intent = new Intent ();
>
> > intent.setAction ("android.intent.action.VIEW");
> > intent.addCategory ("android.intent.category.BROWSABLE");
> > intent.setData (Uri.parse ("http://www.cnn.com";));
>
> > context.startActivity (intent);
>
> > are you happy? :-)
>
> > J
>
> > >I am trying to create a widget that, when clicked on, launches the
> > >browser and goes to a particular URL. Unfortunately, I have had a lot
> > >of problems. I started with a (working) widget that, when clicked,
> > >opened the alarm clock. Then, using the code from another Groups post
> > >(http://groups.google.com/group/android-developers/msg/
> > >11e54b1df3a4d279), I tried to convert it to open the browser instead.
> > >Here is my (non-working) code:
>
> > >public class Widget extends AppWidgetProvider
> > >{
> > >     public void onReceive(Context context, Intent intent)
> > >     {
> > >         String action = intent.getAction();
> > >         if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action))
> > >         {
>
> > >             RemoteViews views = new RemoteViews(context.getPackageName
> > >(), R.layout.widget);
>
> > >             Intent i = new Intent(Intent.ACTION_VIEW).addCategory
> > >(Intent.CATEGORY_BROWSABLE).setComponent(new ComponentName
> > >("com.android.browser", "com.android.browser.BrowserActivity"));
> > >             Uri url = new Uri("http://www.cnn.com";);
> > >             i.setData(url);
> > >             PendingIntent pendingIntent = PendingIntent.getActivity
> > >(context, 0, i, 0);
> > >             views.setOnClickPendingIntent(R.id.Widget, pendingIntent);
>
> > >             AppWidgetManager.getInstance(context).updateAppWidget
> > >(intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS),
> > >views);
> > >         }
> > >     }
> > >}
>
> > >First, I get an error when I try to create the new Uri -- "Cannot
> > >instantiate the type Uri." The Groups post I've been referencing uses
> > >ContentURI - a class that doesn't seem to exist. There is a class
> > >called ContentURIs, but I couldn't figure out a way for that to help
> > >me. The section in the dev guide on intents doesn't really say what a
> > >Uri is or how to create one, and if there's no constructor I'm rather
> > >befuddled.
>
> > >My goal is to just launch a web browser, but I inexplicably haven't
> > >found a single "Hello, Web Browser!" app anywhere. I'd be happy to
> > >abandon this code and start anew or to fix whatever the problem is
> > >with a Uri. If anybody has any advice to offer, I'll be the happiest
> > >girl in the world. Thanks!
>
> > >PS - Unless it's bad advice... then I'll be sad.
>
> > >(Note: This code is a modification of the excellent alarm clock widget
> > >tutorial at
> > >http://nexsoftware.net/wp/2009/07/29/tutorial-creating-a-custom-analo...)
>
> > >--
> > >You received this message because you are subscribed to the Google
> > >Groups "Android Developers" group.
> > >To post to this group, send email to android-developers@googlegroups.com
> > >To unsubscribe from this group, send email to
> > >android-developers+unsubscr...@googlegroups.com
> > >For more options, visit this group at
> > >http://groups.google.com/group/android-developers?hl=en
>
> > --
> > jason.vp.engineering.particle
>
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to