1

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

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();
        }
    }
}

在第一次安装时,它似乎工作得很好。但是,事件开始后,它不会抓取任何未来的事件。扩展程序不会自行刷新是否有原因?

4

1 回答 1

1

您如何触发进一步的更新?您需要手动指定您想要onUpdateData调用的时间,例如内容提供程序发生更改时,或屏幕打开时等。默认情况下,扩展仅每 30 分钟左右刷新一次。有关示例代码,请参阅内置日历扩展的源代码。

于 2013-10-09T23:35:05.967 回答