在Clickable Widget中获取点击事件的问题

时间:2011-08-08 14:30:04

标签: android widget

我写了一个可点击的小部件。我可以看到小部件。但我无法检测到该窗口小部件中的click事件。如果我单击小部件,则应调用onReceive方法。但它没有被调用。任何人都可以指导我可能出现的问题。

源代码

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

public class MyWidget extends AppWidgetProvider {
    public static int IDs[];
    Context context;
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        super.onReceive(context, intent);

        System.out.println("...inside receive11....");

        if (intent.getAction().equals("com.fedorvlasov.lazylist.NEXT_CLICK")) {
            System.out.println("Back Button Clicked ::::::::::::  ");

            onUpdate(context, AppWidgetManager.getInstance(context), IDs);
            // views = new RemoteViews(context.getPackageName(), R.layout.main);
        }
        if (intent.getAction().equals(
                "com.fedorvlasov.lazylist.NEXT_CLICK_NEXT")) {
            System.out.println("Onion Button Clicked Button:::::::::::: ");

            onUpdate(context, AppWidgetManager.getInstance(context), IDs);
            // views = new RemoteViews(context.getPackageName(), R.layout.main);
        }
        if (intent.getAction()
                .equals("com.fedorvlasov.lazylist.TEXTVIEW_CLICK")) {
            System.out.println("Next Button Clicked ::::::::::::  ");

            onUpdate(context, AppWidgetManager.getInstance(context), IDs);
        }
        System.out.println("...out to receive11....");

    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        // TODO Auto-generated method stub
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        IDs = appWidgetIds;
        this.context = context;
        System.out.println("...inside update....");
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.mywidget);

        Intent buttonPrevious = new Intent(context, MyWidget.class);
        buttonPrevious.setAction("com.fedorvlasov.lazylist.NEXT_CLICK");
        PendingIntent pendingIntentButtonP = PendingIntent.getBroadcast(
                context, 0, buttonPrevious, 0);
        remoteViews.setOnClickPendingIntent(R.id.mywidget_left,
                pendingIntentButtonP);

        Intent buttonMiddle = new Intent(context, MyWidget.class);
        buttonMiddle.setAction("com.fedorvlasov.lazylist.TEXTVIEW_CLICK");
        PendingIntent pendingIntentButtonM = PendingIntent.getBroadcast(
                context, 0, buttonMiddle, 0);
        remoteViews.setOnClickPendingIntent(R.id.mywidget_middle,
                pendingIntentButtonM);

        Intent buttonNext = new Intent(context, MyWidget.class);
        buttonNext.setAction("com.fedorvlasov.lazylist.NEXT_CLICK_NEXT");
        PendingIntent pendingIntentButtonN = PendingIntent.getBroadcast(
                context, 0, buttonNext, 0);
        remoteViews.setOnClickPendingIntent(R.id.mywidget_right,
                pendingIntentButtonN);


    }
}

在清单文件

<receiver android:name=".MyWidget" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                <action android:name="com.fedorvlasov.lazylist.NEXT_CLICK" />
                <action android:name="com.fedorvlasov.lazylist.NEXT_CLICK_NEXT" />
                <action android:name="com.fedorvlasov.lazylist.TEXTVIEW_CLICK" />
            </intent-filter>
            <meta-data android:name="android.appwidget.provider"
                android:resource="@xml/widget_info" android:process=":remote" />
        </receiver>

2 个答案:

答案 0 :(得分:3)

您忘记在onUpdate()方法中更新小部件。

在onUpdate()方法

的末尾写appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);

答案 1 :(得分:0)

您必须将每个<action />标记放在清单中的单独<intent-filter />标记内。

这应该是一个错误,因为文档声明您可以在过滤器标记中放置多个操作:

  

应包含零个或多个动作[..]标签   在里面描述过滤器的内容。

Source