Android Widget Button仅触发一次

时间:2014-09-18 11:28:19

标签: android android-widget android-appwidget

我的小部件包含一些文本视图和一个按钮。 按下按钮会调用填充文本视图的Web服务。

这样可以正常工作,但按钮只执行一次,然后在第一次按下后不执行任何操作。 logcat中没有错误。

我删除了webservice方法,使其更易于阅读。下面的代码仍然显示相同的问题:按下按钮仅触发一次。

有人能帮忙吗?

编辑:我认为问题可能是我没有广播接收器?

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

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class DashboardWidget extends AppWidgetProvider {
    static String response = "";
    static clsDashboardResponse result;
    static Context cont;
    static AppWidgetManager appWidgMan;
    static int appWidgID = 0;
    static String count = "";

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // There may be multiple widgets active, so update all of them
        final int N = appWidgetIds.length;
        for (int i=0; i<N; i++) {
            updateAppWidget(context, appWidgetManager, appWidgetIds[i]);
        }
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.dashboard_widget);
        remoteViews.setOnClickPendingIntent(R.id.widget_button, buildButtonPendingIntent(context));
    }

    public static PendingIntent buildButtonPendingIntent(Context context) {
        Intent intent = new Intent();
        return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    }

    @Override
    public void onEnabled(Context context) {
    }

    @Override
    public void onDisabled(Context context) {
    }

    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
            int appWidgetId) {

        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.dashboard_widget);
        cont = context;
        appWidgMan = appWidgetManager;
        appWidgID = appWidgetId;
        count = count + "a";
        views.setTextViewText(R.id.txvWidgetSales, count);

        Calendar cal = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("d MMM yyyy 'at' h:mm:ss a");
        views.setTextViewText(R.id.txvWidgetDate, format.format(cal.getTime()));

        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}

0 个答案:

没有答案
相关问题