1

Possible Duplicate:
How to launch activity from android home screen widget

Whats the simplest code to launch an activity from the applications home-screen widget?

The code I have so far doesnt work and nothing shows in LogCat..??

The widget has an image within it and when the image is tapped, should launch the application. Looking for a snippet.

public class MyWidget extends AppWidgetProvider
{
    // Create an Intent to launch activity from ImageView
    @Override
    public void onEnabled(Context context)
    {
    Intent intent = new Intent(Intent.ACTION_MAIN, null);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);

    ComponentName comp = new ComponentName(context.getPackageName(),MyWidget.class.getName());
    intent.setComponent(cn);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent myPI = PendingIntent.getActivity(context, 0, intent, 0);

    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.content);//<-THE LAYOUT W/I THE MainActivity CLASS??

    views.setOnClickPendingIntent(R.id.widgetLauncher, myPI);//<-THE IMAGEVIEW ID??

    AppWidgetManager mgr = AppWidgetManager.getInstance(context);
    mgr.updateAppWidget(comp, views);
    }
}

Widget Layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/widgetLauncher"
        android:layout_width="258dp"
        android:layout_height="54dp"
        android:layout_centerInParent="true"
        android:src="@drawable/wgt_logo" >
    </ImageView>

</RelativeLayout>
Community
  • 1
  • 1
  • http://buildmobile.com/how-to-code-an-android-widget – slayton Apr 05 '12 at 17:15
  • @Colin Pickard been trying various ways but nothing. See added code above –  Apr 05 '12 at 18:00
  • Whats with the negative marks? I've done the research and I cant get anything to work. I added more code. Whatever people. –  Apr 05 '12 at 18:02

1 Answers1

6

This finally worked:

public class MyWidget extends AppWidgetProvider
{
    // Create an Intent to launch activity
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds)
    {
    super.onUpdate(context, appWidgetManager, appWidgetIds);

    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.home_widget);
    // When we click the widget, we want to open our main activity.
    Intent launchActivity = new Intent(context, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, launchActivity, 0);
    remoteViews.setOnClickPendingIntent(R.id.widget, pendingIntent);;

    ComponentName thisWidget = new ComponentName(context, MyWidget.class);
    AppWidgetManager manager = AppWidgetManager.getInstance(context);
    manager.updateAppWidget(thisWidget, remoteViews);
    }
}
  • 1
    But this seems to set an onclick for the entire widget, not just the image? How does one have a separate event for clicking on the widget and clicking on an image within the widget? – Yusufk Jul 12 '14 at 12:45