2

I'm now developing a simple widget. Its really new for me, I'm not really getting how to use the AppWidgetProvider

My current widget is only displaying image which it will directly link to a website when user click it.

So, my question is, which of these should I use in AppWidgetProvider?

As we know there are 4 of them.

  • onDeleted(context)
  • onDisabled(context)
  • onUpdated(context)
  • onReceived(context)

My current code for it is as below

 public class ExampleAppWidgetProvider extends AppWidgetProvider {

    private static ImageView img;

    public static void updateAppWidget(final Context context,
            AppWidgetManager appWidgetManager, int appWidgetId) {

        img = (ImageView) findViewById(R.id.Image);
        img.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri
                        .parse("http://www.google.com"));
                PendingIntent.getActivity(context, 0, intent, 0);
            }

        });
    }

    private static ImageView findViewById(int image) {
        // TODO Auto-generated method stub
        return null;
    }

}

Can u see any mistake I have made? The problem is that, the image could not link to the website when I click it.

I have created the internet permission in manifest. Please help me.

THE SOLUTION (I managed to run my project using these :) tq friends for helping )

public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        for (int i = 0; i < appWidgetIds.length; i++) {
            int appWidgetId = appWidgetIds[i];

            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.legoland.com.my"));
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                    intent, 0);

            RemoteViews views = new RemoteViews(context.getPackageName(),
                    R.layout.widget1);
            views.setOnClickPendingIntent(R.id.Image, pendingIntent);
            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
4

1 に答える 1

2

アプリ ウィジェットでは、 RemoteViews.[setOnClickPendingIntent ](http://developer.android.com/reference/android/widget/RemoteViews.html#setOnClickPendingIntent(int, android.app.PendingIntent))を使用する必要があります。

public static void updateAppWidget(final Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
    RemoteViews v = new RemoteViews(context.getPackageName(), R.layout.your_layout);
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
    v.setOnClickPendingIntent(R.id. Image, PendingIntent.getActivity(context, 0, intent, 0));

    appWidgetManager.updateAppWidget(new ComponentName(context, getClass()), views);
}
于 2012-12-13T08:26:22.067 に答える