小部件中的文本不会更改

时间:2014-05-03 17:26:23

标签: android android-layout android-widget textview

我目前正在开发一个小部件,它可以计算到学校什么时候开始。我已经对它进行了测试,这一切都与一件事有关;窗口小部件中的文本不会更改

这是我的代码(BGSWidgetProvider.java):

package com.bordengrammar.bordengrammarapp;

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

import com.bordengrammar.bordengrammarapp.utils.ut;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

public class BGSWidgetProvider extends AppWidgetProvider {

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

        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1, 1000);
    }

    private class MyTime extends TimerTask {
        RemoteViews remoteViews;
        AppWidgetManager appWidgetManager;
        ComponentName thisWidget;       

        public MyTime(Context context, AppWidgetManager appWidgetManager) {
            this.appWidgetManager = appWidgetManager;
            remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
            thisWidget = new ComponentName(context, BGSWidgetProvider.class);
        }

        @Override
        public void run() {
            remoteViews.setTextViewText(R.id.hours, hour());
            remoteViews.setTextViewText(R.id.minutes, minutes());
            ut.logIt(minutes());
            ut.logIt(hour());
            appWidgetManager.updateAppWidget(thisWidget, remoteViews);
        }




        public String hour() {
            /*
            * Work out whether tommorow is right day, (Right meaning mon-fri)
            * If it is work out the amount of hours and minutes until 8.30am tommorow
            * Else work out how many days, hours and minutes until monday 8.30am
            *
             */
            String result;
            Calendar now = Calendar.getInstance();
            int currentDay = now.get(Calendar.DAY_OF_WEEK);

            Calendar school = Calendar.getInstance();
            school.add(Calendar.DAY_OF_YEAR, 1);
            if (currentDay == Calendar.SATURDAY)
            {
                school.add(Calendar.DAY_OF_YEAR, 1);
            }
            else if (currentDay == Calendar.FRIDAY)
            {
                school.add(Calendar.DAY_OF_YEAR, 2);
            }

            school.set(Calendar.HOUR_OF_DAY, 8);
            school.set(Calendar.MINUTE, 30);
            long millisLeft = school.getTimeInMillis() - now.getTimeInMillis();
            long hoursLeft = millisLeft / (60 * 60 * 1000); 
            String hours = Long.toString(hoursLeft);        
            return hours;


        }
        public String minutes() {
            /*
            * Work out whether tommorow is right day, (Right meaning mon-fri)
            * If it is work out the amount of hours and minutes until 8.30am tommorow
            * Else work out how many days, hours and minutes until monday 8.30am
            *
             */         
            Calendar now = Calendar.getInstance();
            int currentDay = now.get(Calendar.DAY_OF_WEEK);

            Calendar school = Calendar.getInstance();
            school.add(Calendar.DAY_OF_YEAR, 1);
            if (currentDay == Calendar.SATURDAY)
            {
                school.add(Calendar.DAY_OF_YEAR, 1);
            }
            else if (currentDay == Calendar.FRIDAY)
            {
                school.add(Calendar.DAY_OF_YEAR, 2);
            }

            school.set(Calendar.HOUR_OF_DAY, 8);
            school.set(Calendar.MINUTE, 30);

            long millisLeft = school.getTimeInMillis() - now.getTimeInMillis();         
            long minutesLeft =  (millisLeft % (60 * 60 * 1000)) / (60 * 1000);          
            String minutes = Long.toString(minutesLeft);


            return minutes;


        }



    }

我的布局(widget_layout.xml):

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/layout"
              android:orientation="horizontal"
              android:layout_width="250dp"
              android:layout_height="72dp"
              android:background="@color/bordenpurple">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="10"
        android:textSize="60dp"
        android:paddingTop="5dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:textColor="@color/bordenyellow"
        android:typeface="sans"
        android:id="@+id/hours"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Hours"
        android:id="@+id/textView"
        android:textColor="@color/bordenyellow"
        android:padding="5dp"
        android:layout_marginLeft="-10dp"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="10"
        android:textSize="60dp"
        android:paddingTop="5dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:textColor="@color/bordenyellow"
        android:typeface="sans"
        android:id="@+id/minutes"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Minutes"
        android:id="@+id/textView3"
        android:textColor="@color/bordenyellow"
        android:padding="5dp"
        android:layout_marginLeft="-7dp"
        />

</LinearLayout>

正如你所看到的,我在run方法中放了一个日志语句来测试我的代码,它运行起来,这是我的logcat:

05-03 18:24:30.874    8011-8029/com.bordengrammar.bordengrammarapp E/Logger﹕ 38
05-03 18:24:31.878    8011-8029/com.bordengrammar.bordengrammarapp E/Logger﹕ 6
05-03 18:24:31.878    8011-8029/com.bordengrammar.bordengrammarapp E/Logger﹕ 38
05-03 18:24:32.878    8011-8029/com.bordengrammar.bordengrammarapp E/Logger﹕ 6
05-03 18:24:32.878    8011-8029/com.bordengrammar.bordengrammarapp E/Logger﹕ 38
05-03 18:24:33.878    8011-8029/com.bordengrammar.bordengrammarapp E/Logger﹕ 6
05-03 18:24:33.878    8011-8029/com.bordengrammar.bordengrammarapp E/Logger﹕ 38
05-03 18:24:34.878    8011-8029/com.bordengrammar.bordengrammarapp E/Logger﹕ 6
05-03 18:24:34.878    8011-8029/com.bordengrammar.bordengrammarapp E/Logger﹕ 38
05-03 18:24:35.874    8011-8029/com.bordengrammar.bordengrammarapp E/Logger﹕ 6
05-03 18:24:35.874    8011-8029/com.bordengrammar.bordengrammarapp E/Logger﹕ 38
05-03 18:24:36.878    8011-8029/com.bordengrammar.bordengrammarapp E/Logger﹕ 6
05-03 18:24:36.878    8011-8029/com.bordengrammar.bordengrammarapp E/Logger﹕ 38

我不知道如何解决这个问题,任何帮助都会受到赞赏!

1 个答案:

答案 0 :(得分:0)

永远不要在小部件中使用TimerAppWidgetProviderBroadcastReceiver,它意味着只能在很短的时间内保持活动状态来处理它收到的广播意图,然后它可以在广播之后的任何时刻被销毁已经处理并且onUpdate()方法返回(但是小部件当然会保持可见)。

Android通常会每隔30分钟调用您的小部件更新代码,因此您不必担心手动刷新它,只需将显示代码(目前位于run())移至onUpdate()