0

我需要每天在指定时间更新我的小部件。下次更新必须在 24 小时内进行。我使用updatePeriodMillis过,但它对不同用户的工作方式不同。所以我决定实施强大的解决方案。我找到了这个像这样实现了 CountDownTimer:

public class RepeatService extends Service
{
    FrequentlyUsedMethods faq = new FrequentlyUsedMethods();
     RemoteViews view; 
    @Override
    public void onCreate()
    {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        buildUpdate();

        return super.onStartCommand(intent, flags, startId);
    }

    private void buildUpdate()
    {
        String lastUpdated = DateFormat.format("hh:mm:ss", new Date()).toString();
        Log.i("lastUpdated time", FrequentlyUsedMethods.getCurrentDate());
        view = new RemoteViews(getPackageName(), R.layout.main_widget);

        MyCount counter = new MyCount(5000,1000);
        counter.start();

        ComponentName thisWidget = new ComponentName(this, Widget.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(this);
        manager.updateAppWidget(thisWidget, view);
    }

    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }
    public class MyCount extends CountDownTimer{
        public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
            }
        @Override
        public void onFinish() {
            // TODO Auto-generated method stub
             view.setTextViewText(R.id.widget_button, "done");
        }

        @Override
        public void onTick(long millisUntilFinished) {
            // TODO Auto-generated method stub
             view.setTextViewText(R.id.widget_button,Long.toString(millisUntilFinished/1000));

        }
    }

}

但这对我不起作用。我该如何解决?也许通过Handler使用?还是通过IBind实施?

4

2 回答 2

0

我使用 AlarmManager 和 Service 解决了这个问题。可以贴代码,如果有人需要...

于 2013-07-12T14:16:35.553 回答
0

这是我的解决方案: 服务:

RepeatService  extends Service
{
    @Override
    public void onCreate()
    {
        super.onCreate();

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {

        buildUpdate(intent);

        return super.onStartCommand(intent, flags, startId);
    }

    private void buildUpdate(Intent intent)
    {

          setWidgetActive(context,true, Constants.WIDGET_UPDATE);

          Intent archive = new Intent(context, Widget.class);
          archive.setAction(Constants.ACTION_WIDGET_ARCHIVE);


          ArrayList<Quote> quotes  = new ArrayList<Quote>();
          quotes = faq.getQuotes(getApplicationContext());
          remoteViews.setTextViewText(R.id.widget_textview,faq.getCurrentString());
          faq.sendNotification(quotes.get(quotes.size()-1));
          ComponentName thisWidget = new ComponentName(this, Widget.class);
          AppWidgetManager manager = AppWidgetManager.getInstance(this);
          manager.updateAppWidget(thisWidget, remoteViews);

    }
}

小部件:

public class Widget  extends AppWidgetProvider {

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

            FrequentlyUsedMethods faq = new FrequentlyUsedMethods(context);

            if(!getWidgetActive(context, Constants.WIDGET_ADDED))
            {
            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);  
            ComponentName thisAppWidget = new ComponentName(context.getPackageName(), Widget.class.getName());

            }
            setWidgetActive(context,true, Constants.WIDGET_ACTIVE);
            setWidgetActive(context,true, Constants.WIDGET_ADDED);

        }
        @Override
           public void onDeleted(Context context, int[] appWidgetIds) {
                   super.onDeleted(context, appWidgetIds);
           }
        @Override
        public void onDisabled(Context context) {
            setWidgetActive(context,false, Constants.WIDGET_ACTIVE);
            final AlarmManager m = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);  
            if (service != null)
            {
                m.cancel(service);
            }
            super.onDisabled(context);
        }
      @Override
      public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

           final AlarmManager m = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

          Calendar calendar = Calendar.getInstance();
          calendar.clear();
          long l  = System.currentTimeMillis() + 300;
            Intent i = new Intent(context, RepeatService.class);


           if (service == null)
           {
               service = PendingIntent.getService(context, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
           }


           m.setRepeating(AlarmManager.RTC_WAKEUP, l, 1000 * 15, service);


      }
}
于 2014-01-01T12:59:18.373 回答