Another quick Android tutorial. I couldn’t find an easy or correct method of launching a browser when you click on a homescreen widget. Well, here it is…

  1. public class clickWidget extends AppWidgetProvider
  2. {
  3. @Override
  4. public void onUpdate( Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds )
  5. {
  6. RemoteViews remoteViews =
  7.    new RemoteViews( context.getPackageName(), R.layout.widget );
  8. remoteViews.setImageViewResource(R.id.ImageView01, drawableResourse);
  9.  
  10. ComponentName myWidget =
  11.    new ComponentName( context, clickWidget.class );
  12.  
  13. // Create an Intent to launch Browser
  14. Intent intent =
  15.    new Intent(
  16.       Intent.ACTION_VIEW, Uri.parse("http://example.com")
  17.    );
  18. PendingIntent pendingIntent =
  19.    PendingIntent.getActivity(context, 0, intent, 0);    
  20.  
  21. remoteViews.setOnClickPendingIntent(R.id.ImageView01, pendingIntent);
  22. appWidgetManager.updateAppWidget( myWidget, remoteViews);
  23. }

I’ve used this as the basis of a demo widget – “MI5 Terror Threat Level”. The widget displays the UK’s Threat Level on your homescreen. Clicking on it takes you to the MI5 page discussing the threat level.

The threat level is determined by parsing the RSS that the security services so helpfully provide. At the moment, the widget keeps a local copy of the graphics because the RSS feed contains references to “localhost” images.

You can download the widget by scanning in this QR code.

MI5 Widget - QR Code

MI5 Widget - QR Code

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

   
© 2011 Terence Eden has a BlogSuffusion theme by Sayontan Sinha

Switch to our mobile site