我正在制作一个简单的Dashclock日历扩展,它停止更新

时间:2013-10-09 02:05:09

标签: android service dashclock

我正在制作一个基本的Dashclock扩展程序,轮询CalendarContract.Events以查找同步到用户设备的所有日历事件的列表,检索计划最快发生的日历事件,并发布时间,标题,位置,和desctiption。这是我的代码:

public class FullCalService extends DashClockExtension {
    public static final String[] FIELDS = { Events._ID, Events.TITLE,
            Events.ALL_DAY, Events.EVENT_LOCATION, Events.DTSTART,
            Events.EVENT_TIMEZONE, Events.DESCRIPTION };

    public FullCalService() {
    }

    @Override
    protected void onUpdateData(int arg0) {
        TimeZone tz = TimeZone.getDefault();
        long currentTimeMillis = Calendar.getInstance().getTimeInMillis() - tz.getRawOffset();
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(this);
        Cursor c;
        if (prefs.getBoolean("allDayAllowed", false)) {
            c = getContentResolver().query(
                    Events.CONTENT_URI,
                    FIELDS,
                    new StringBuilder().append("(").append(Events.DTSTART)
                            .append(" >= ?)").toString(),
                    new String[] { Long.toString(currentTimeMillis) },
                    Events.DTSTART, null);
        } else {
            c = getContentResolver().query(
                    Events.CONTENT_URI,
                    FIELDS,
                    new StringBuilder().append("((").append(Events.ALL_DAY)
                            .append("= ?) AND (").append(Events.DTSTART)
                            .append(" >= ?))").toString(),
                    new String[] { Integer.toString(0),
                            Long.toString(currentTimeMillis) }, Events.DTSTART,
                    null);
        }
        if (c.moveToFirst()) {
            long eventTimeMillis = c.getLong(c.getColumnIndex(Events.DTSTART));
//          if (tz.inDaylightTime(new Date(eventTimeMillis))) {
//              eventTimeMillis += tz.getDSTSavings();
//          }
            //Log.d("DesCal service", "Value of hoursToReveal: "+prefs.getString("hoursToReveal", "1"));
            if (eventTimeMillis < currentTimeMillis + 360000
                    * Integer.parseInt(prefs.getString("hoursToReveal", "1"))) {
                String title = c.getString(c.getColumnIndex(Events.TITLE));
                String loc = c.getString(c
                        .getColumnIndex(Events.EVENT_LOCATION));
                String time = DateUtils.formatDateTime(this, eventTimeMillis,
                        DateUtils.FORMAT_SHOW_TIME);
                String desc = c.getString(c.getColumnIndex(Events.DESCRIPTION));
                StringBuilder expandedBody = new StringBuilder(time);
                if (!loc.isEmpty()){
                    expandedBody.append(" - ").append(loc);
                }
                expandedBody.append("\n").append(desc);
                String uri = new StringBuilder(
                        "content://com.android.calendar/events/").append(
                        c.getLong(c.getColumnIndex(Events._ID))).toString();
                publishUpdate(new ExtensionData()
                        .visible(true)
                        .status(time)
                        .expandedTitle(title)
                        .expandedBody(expandedBody.toString())
                        .icon(R.drawable.ic_dash_cal)
                        .clickIntent(
                                new Intent(Intent.ACTION_VIEW, Uri.parse(uri))));
                c.close();
            } else {
                publishUpdate(new ExtensionData().visible(false));
                c.close();
            }
        } else {
            publishUpdate(new ExtensionData().visible(false));
            c.close();
        }
    }
}

首次安装时,它似乎工作得很好。然而,在活动开始后,它不会抓住任何未来的事件。有没有理由为什么扩展不会刷新自己?

1 个答案:

答案 0 :(得分:1)

您是如何触发进一步更新的?您需要手动指定何时调用onUpdateData,例如当内容提供商发生更改时,或者屏幕打开等时,默认情况下,扩展只会每30分钟左右刷新一次。请参阅built in calendar extension的来源,例如代码。