我需要每天在指定的时间更新我的小部件。下一次更新必须在24小时内完成。
我使用updatePeriodMillis
,但它对不同的用户有不同的作用。所以我决定实施强大的解决方案。我找到了这个one并实现了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
实施?
答案 0 :(得分:0)
我用AlarmManager和Service解决了这个问题。如果有人需要,可以发布代码......
答案 1 :(得分: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);
}
}