Android小部件的按钮单击事件

Sha*_*our 43 android android-widget

我有一个android小部件,每隔10分钟从服务器获取数据并在屏幕上显示它.
我想为该小部件添加一个"刷新"按钮.
当用户单击该按钮时,我想运行从服务器获取信息的方法.
将事件处理程序添加到应用程序中的按钮非常容易,但是我找不到窗口小部件的示例.
我想帮助为小部件中的按钮单击添加功能.

Ert*_*maa 71

这是一个应该帮助的例子:

package com.automatic.widget;

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

public class Widget extends AppWidgetProvider {

    private static final String SYNC_CLICKED    = "automaticWidgetSyncButtonClick";

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        RemoteViews remoteViews;
        ComponentName watchWidget;

        remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        watchWidget = new ComponentName(context, Widget.class);

        remoteViews.setOnClickPendingIntent(R.id.sync_button, getPendingSelfIntent(context, SYNC_CLICKED));
        appWidgetManager.updateAppWidget(watchWidget, remoteViews);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        super.onReceive(context, intent);

        if (SYNC_CLICKED.equals(intent.getAction())) {

            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

            RemoteViews remoteViews;
            ComponentName watchWidget;

            remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
            watchWidget = new ComponentName(context, Widget.class);

            remoteViews.setTextViewText(R.id.sync_button, "TESTING");

            appWidgetManager.updateAppWidget(watchWidget, remoteViews);

        }
    }

    protected PendingIntent getPendingSelfIntent(Context context, String action) {
        Intent intent = new Intent(context, getClass());
        intent.setAction(action);
        return PendingIntent.getBroadcast(context, 0, intent, 0);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你这么好的例子我已经使用了这个代码和我的应用程序正常工作......很多 (2认同)
  • 关于`PendingIntent.getBroadcast(context,0,intent,0)的说明`:如果你有多个小部件实例,这将导致错误.问题:每当您单击任何小部件实例时,只有最后一个小部件会更新.解决方案:将widgetId传递给`getPendingSelfIntent`并尝试:`PendingIntent.getBroadcast(context,widgetId,intent,0)`[详细说明](http://stackoverflow.com/a/13487623/1493081) (2认同)

Sha*_*our 45

我发现了怎么做.在> 标记中
AndroidManifest.xml文件添加操作<receiver><intent-filter>:

<action android:name="MY_PACKAGE_NAME.WIDGET_BUTTON" />
Run Code Online (Sandbox Code Playgroud)

在提供程序中添加与操作名称匹配的常量:

public static String WIDGET_BUTTON = "MY_PACKAGE_NAME.WIDGET_BUTTON";
Run Code Online (Sandbox Code Playgroud)

在该onUpdate()方法中添加与操作匹配的待处理意图:

Intent intent = new Intent(WIDGET_BUTTON);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.MY_BUTTON_ID, pendingIntent );
Run Code Online (Sandbox Code Playgroud)

最后,在onRecieve()方法中,检查操作名称:

 if (WIDGET_BUTTON.equals(intent.getAction())) {
//your code here

    }
Run Code Online (Sandbox Code Playgroud)


Joh*_*ley 11

这是另一个答案,具有以下好处:

  • 它处理所有App Widget实例(用户可能在屏幕上有各种配置/大小的窗口小部件的多个实例).所有实例的编码都是官方文档规定的内容.请参阅指南>应用程序窗口小部件>使用AppWidgetProvider类,向下滚动到"ExampleAppWidgetProvider"的代码示例.
  • onReceive实际调用的主力代码onUpdate(因此减少了代码重复).
  • 代码输入onUpdate(Context context)是通用的,因此可以将其放入任何AppWidgetProvider子类中.

代码:

public class MyWidget extends AppWidgetProvider {

    private static final String ACTION_UPDATE_CLICK = 
              "com.example.myapp.action.UPDATE_CLICK";

    private static int mCount = 0;

    private static String getMessage() {
        return String.valueOf(mCount++);
    }

    private PendingIntent getPendingSelfIntent(Context context, String action) {
        // An explicit intent directed at the current class (the "self").
        Intent intent = new Intent(context, getClass());
        intent.setAction(action);
        return PendingIntent.getBroadcast(context, 0, intent, 0);
    }

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

        String message = getMessage();

        // Loop for every App Widget instance that belongs to this provider.
        // Noting, that is, a user might have multiple instances of the same
        // widget on
        // their home screen.
        for (int appWidgetID : appWidgetIds) {
            RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                                                      R.layout.my_widget);

            remoteViews.setTextViewText(R.id.textView_output, message);
            remoteViews.setOnClickPendingIntent(R.id.button_update,
                                                getPendingSelfIntent(context,
                                                           ACTION_UPDATE_CLICK)
            );

            appWidgetManager.updateAppWidget(appWidgetID, remoteViews);

        }
    }

    /**
     * A general technique for calling the onUpdate method,
     * requiring only the context parameter.
     *
     * @author John Bentley, based on Android-er code.
     * @see <a href="http://android-er.blogspot.com
     * .au/2010/10/update-widget-in-onreceive-method.html">
     * Android-er > 2010-10-19 > Update Widget in onReceive() method</a>
     */
    private void onUpdate(Context context) {
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance
                (context);

        // Uses getClass().getName() rather than MyWidget.class.getName() for
        // portability into any App Widget Provider Class
        ComponentName thisAppWidgetComponentName =
                new ComponentName(context.getPackageName(),getClass().getName()
        );
        int[] appWidgetIds = appWidgetManager.getAppWidgetIds(
                thisAppWidgetComponentName);
        onUpdate(context, appWidgetManager, appWidgetIds);
    }

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

        if (ACTION_UPDATE_CLICK.equals(intent.getAction())) {
            onUpdate(context);
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

小部件看起来像这样

小部件更新按钮示例. 简单计数.

这建立在getPendingSelfIntent@Kels,@ SharonHaimPour和@ Erti-ChrisEelmaa 的工作之上.

它还基于Android-er> 2010-10-19> onReceive()方法(而不是我)中的Update Widget构建,其中演示了如何在App Widget实例的基础上从onReceive调用onUpdate.我将该代码编成通用并将其包装callOnUpdate.


Kel*_*els 10

protected PendingIntent getPendingSelfIntent(Context context, String action) {
    Intent intent = new Intent(context, getClass());
    intent.setAction(action);
    return PendingIntent.getBroadcast(context, 0, intent, 0);
}

views.setOnClickPendingIntent(R.id.Timm, getPendingSelfIntent(context,
                              "ham"));
Run Code Online (Sandbox Code Playgroud)

也喜欢网址:

如何正确处理Widget上的点击事件

如果您以不同的方式解决它,请提供此答案