onReceive没有在Android Widget中调用

时间:2011-03-13 07:14:16

标签: java android android-widget

按下按钮时,我无法让我的代码在我的小部件中点击onReceive方法。我很确定我在清单中的所有设置都正确,所有代码都正确。我是否遗漏了某些内容,或者我是否需要添加一些内容才能使其生效?

清单

<?xml version="1.0" encoding="utf-8"?>

    

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <receiver android:name="Widget">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            <action android:name="com.sense.widget.Widget.REFRESH" />
            <action android:name="com.sense.widget.Widget.PAGE_NEXT" />
            <action android:name="com.sense.widget.Widget.PAGE_PREV" /> 
        </intent-filter>

        <meta-data android:name="android.appwidget.provider"
            android:resource="@xml/widget_provider" />
    </receiver>
</application>

代码

public class Widget extends AppWidgetProvider {
public static String REFRESH = "Refresh";
public static String PAGE_NEXT = "Next";
public static String PAGE_PREV = "Previous";
RemoteViews remoteViews;
ComponentName kWidget;

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {

    remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);

    Intent intent = new Intent(context, getClass());
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
            intent, 0);

    intent.setAction(REFRESH);
    pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
    remoteViews.setOnClickPendingIntent(R.id.refresh, pendingIntent);

    intent = new Intent(context, getClass());
    intent.setAction(PAGE_NEXT);
    pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
    remoteViews.setOnClickPendingIntent(R.id.next, pendingIntent);

    intent = new Intent(context, getClass());
    intent.setAction(PAGE_PREV);
    pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
    remoteViews.setOnClickPendingIntent(R.id.prev, pendingIntent);

    kWidget = new ComponentName(context, Widget.class);
    super.onUpdate(context, appWidgetManager, appWidgetIds);
    appWidgetManager.updateAppWidget(kWidget, remoteViews);

}

@Override
public void onReceive(Context context, Intent intent) {

    Toast.makeText(context, "here", Toast.LENGTH_LONG);
    super.onReceive(context, intent);
}

}

提供商

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="294dp"
android:minHeight="294dp"
android:updatePeriodMillis="100000"
android:initialLayout="@layout/main">

2 个答案:

答案 0 :(得分:0)

缺少节目();你的祝酒词: - )

Toast.makeText(context, "here", Toast.LENGTH_LONG).show();

答案 1 :(得分:0)

问题发生在PendingIntent's我使用getActivity代替getBroadcast