2

我正在开发我的第一个小部件,这是一个简单的启动倒计时小部件。它一直运行良好,但我厌倦了每周更新日期和重新部署应用程序。

我的目标是从 Web 服务 (JSON) 中提取日期,并在 updateAppWidget() 中的 LocalDateTime() 中使用返回的结果。如:

LocalDateTime ko = new LocalDateTime($yearJSON, $monthJSON, $dayJSON, 0, 0, 0, 0);

我只是不知道在哪里定义或如何调用我的 AsyncTask 来完成此任务。我有一个 JSONParser 类,我过去曾使用它从网络上提取 JSON 数据,但我只是不确定如何将它实现到我的小部件中。我在下面发布了我的工作代码(没有 AsyncTask),希望有人能帮助我。谢谢!

public class Widget_Provider4_1 extends AppWidgetProvider {
// Custom Intent Name That Is Used By The AlarmManager To Update The Widget On The Top Of Every Hour.
public static String HKC_WIDGET_UPDATE = "xxxxxx.HKC_WIDGET_UPDATE";
// Custom Intent Name That Is Used To Show GBR Toast.
public static String HKC_WIDGET_CLICK = "xxxxxx.HKC_WIDGET_CLICK";


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

    if (HKC_WIDGET_UPDATE.equals(intent.getAction())) {
        // Get The Widget Manager And IDs For This Widget Provider, Then Call The Shared Timer Update Method.
        ComponentName thisAppWidget = new ComponentName(context.getPackageName(), getClass().getName());
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        // Update All Active Widgets
        int ids[] = appWidgetManager.getAppWidgetIds(thisAppWidget);
        for (int appWidgetID: ids) {
            updateAppWidget(context, appWidgetManager, appWidgetID);
        }
    }
    // If Logo Was Clicked Show Toast.
    else if (HKC_WIDGET_CLICK.equals(intent.getAction())) {
        Toast.makeText(context, "Go Team!", Toast.LENGTH_LONG).show();
    }
}


private PendingIntent createClockTickIntent(Context context) {
    // Define Intent To Update Widget
    Intent intent = new Intent(HKC_WIDGET_UPDATE);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    return pendingIntent;
}


@Override
public void onDisabled(Context context) {
    super.onDisabled(context);

    // Disable AlarmManager
    AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(createClockTickIntent(context));
}


@Override
public void onEnabled(Context context) {
    super.onEnabled(context);

    // Define AlarmManager
    AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

    // Update On The Top Of Every Hour
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY)+1);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    alarmManager.setRepeating(AlarmManager.RTC, cal.getTimeInMillis(), 1000*60*60, createClockTickIntent(context));
}


@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    // Define Number Of Current Widgets
    final int N = appWidgetIds.length;

    // Loop Through Each Widget That Belongs To This Provider
    for (int i = 0; i < N; i++) {
        int appWidgetId = appWidgetIds[i];
        // Create an Intent To Launch WidgetActivity - In This Case It Just Shows Toast.
        Intent intent = new Intent(HKC_WIDGET_CLICK);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
        // Get Layout For Widget And Attach An On-Click Listener To The Icon
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout4_1);
        views.setOnClickPendingIntent(R.id.logo, pendingIntent);
        // Tell AppWidgetManager To Perform Intent On Current Widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
        // Update The Timer Using Shared Method
        updateAppWidget(context, appWidgetManager, appWidgetId);
    }
}


public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
    // Define Kickoff Time
    LocalDateTime ko = new LocalDateTime(2013, 8, 31, 0, 0, 0, 0);
    // Define Current Time
    LocalDateTime date = new LocalDateTime();
    // Zero Out Minutes, Seconds, And Milliseconds On Current Time - Fixes 11:00:xx PM Bug
    LocalDateTime cur = new LocalDateTime(date.getYear(), date.getMonthOfYear(), date.getDayOfMonth(), date.getHourOfDay(), 0, 0, 0);

    // Let Joda Date Make All Calculations
    Period period = new Period(ko, cur, PeriodType.standard());

    // Convert Times To Strings And Remove Dashes Used By Joda Time Format (ISO8601)
    String s_months = Integer.toString(period.getMonths()).replace("-","");
    String s_weeks = Integer.toString(period.getWeeks()).replace("-","");
    String s_days = Integer.toString(period.getDays()).replace("-","");
    String s_hours = Integer.toString(period.getHours()).replace("-","");

    // Create Updated Time
    String uTime = s_months+"mo "+s_weeks+"wks "+s_days+"d "+s_hours+"hrs";

    // Convert Strings To Integers For Comparisons
    int months = Integer.parseInt(s_months);
    int weeks = Integer.parseInt(s_weeks);
    int days = Integer.parseInt(s_days);
    int hours = Integer.parseInt(s_hours);

    // If Time Periods Are All <= 0 Then Show "Game Time!"
    if(months <= 0 && weeks <= 0 && days <= 0 && hours <= 0){
        uTime = "Game Time!!!";
    }

    // Update View
    RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout4_1);
    appWidgetManager.updateAppWidget(appWidgetId, updateViews);
    updateViews.setTextViewText(R.id.TimeLeft, uTime);
    appWidgetManager.updateAppWidget(appWidgetId, updateViews);
}

}

4

0 回答 0