Susan wrote:
> 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);
Change the previous three lines to:
Intent i=new Intent(ACTION_VIEW, Uri.parse("http://www.cnn.com"));
> 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.
Check the date. That is probably an 18-month-old post. Uri is in the
android.net package.
> 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!
To launch a Web browser from most places, you need:
startActivity(new Intent(ACTION_VIEW, Uri.parse("http://www.cnn.com")));
In your case, though, you're creating an app widget, which makes things
a wee bit different:
Intent i=new Intent(ACTION_VIEW, Uri.parse("http://www.cnn.com"));
PendingIntent pendingIntent=PendingIntent.getActivity(context, 0, i, 0);
views.setOnClickPendingIntent(R.id.Widget, pi);
That will trigger the Browser when that specific UI widget in the app
widget is tapped.
--
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy
_Android Programming Tutorials_ Version 1.0 In Print!
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en