我需要每天在指定时间更新我的小部件。下次更新必须在 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
实施?