android - 小部件中的 setOnClickPendingIntent 不起作用

标签 android widget click

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

  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 中,我想在单击小部件的 View 时接收它,但它不起作用:

@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 list 接收器:

<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>

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

最佳答案

改变

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

<action android:name="ActionReceiverRefresh"/>

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

关于android - 小部件中的 setOnClickPendingIntent 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17710446/

相关文章:

python - 如何将小部件放入选项卡中以控制选项卡中的绘图

R 传单 Shiny : shape_click$id is NULL

css - 如何使整个 DIV 可点击

java - Android - 以编程方式禁用短信通知

android - kotlin.NotImplementedError: An operation is not implemented: not implemented Error from ImageButton Click

android - 子类化 SurfaceView 并覆盖 onDraw() 以更改 SurfaceView 参数以生成所需大小的预览

android - 如何使用固定大小的webview?

c++ - 如何将自定义小部件添加为 ToolButton 的弹出菜单?

java - 小部件是什么意思并解释使用小部件

jquery - 为什么 if/elseif 语句不起作用 jquery