小部件中的setOnClickPendingIntent不起作用

时间:2013-07-17 21:20:39

标签: android widget click

当我点击它时,我尝试了很多改变我的小部件视图的图像,但它不起作用。这是我的代码:

  public class myAppWidgetProvider extends AppWidgetProvider {

        public static String ACTION_WIDGET_REFRESH = "ActionReceiverRefresh";
         //...

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

            final int N = appWidgetIds.length;

            // Perform this loop procedure for each App Widget that belongs to this provider
            for (int i=0; i<N; i++) {
                int appWidgetId = appWidgetIds[i];

            Intent intent = new Intent(context, myAppWidgetProvider.class);
            intent.setAction(ACTION_WIDGET_REFRESH);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
            RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
            rv.setOnClickPendingIntent(R.id.imageView, pendingIntent);
            appWidgetManager.updateAppWidget(appWidgetId, rv);
            }

在onReceive中,当我点击widget的视图时我想收到它,但它不起作用:

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

        final String action = intent.getAction();
        if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) {

            RemoteViews rmv = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
                   //and this change doesn't work :(( \/
            rmv.setImageViewResource(R.id.imageView, R.drawable.button_power_on_small);
        }
    }

这是我的Android清单接收器:

<receiver android:name="myAppWidgetProvider" android:exported="false" android:icon="@drawable/button_power_off">
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        <action android:name="com.xxx.xxx.myAppWidgetProvider.ACTION_WIDGET_REFRESH"/>
        <action android:name="android.appwidget.action.APPWIDGET_DELETED"/>
        <action android:name="android.media.RINGER_MODE_CHANGED"/>
</intent-filter>
    <meta-data android:name="android.appwidget.provider"
               android:resource="@xml/widget_info_provider" />
</receiver>

如果有人建议我解决方案,我将不胜感激。

1 个答案:

答案 0 :(得分:2)

更改

<action android:name="com.xxx.xxx.myAppWidgetProvider.ACTION_WIDGET_REFRESH"/>

<action android:name="ActionReceiverRefresh"/>

您的BroadcastReceiver正在寻找错误的操作字符串。请记住,AndroidManifest是一个XML文件。你必须把String的实际值;指向Java常量不起作用。