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

ここに私の解決策があります: サービス:

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 に答える