Quantcast
Channel: MobileRead Forums - Kindle Developer's Corner
Viewing all articles
Browse latest Browse all 4408

K4 Timer issues

$
0
0
Good day all,

I was exploring the world of kindle development, and as a simple introductory project (and potentially useful application) I thought I would make a clock app that periodically shows the time on the e-ink screen. (This would be nice to replace a digital alarm clock as the e-ink screen does not produce any ambient light). However, I've run into some difficulties.

Namely, the kindlet library Timer class does not seem to be firing its scheduled tasks. I've tried several variations of the schedule methods and different assumptions about the value it is accepting for delay/period arguments, but nothing seems to be working. It will run its task once, but won't update on the period it is supposed to and only runs subsequent updates when the kindle is awoken from the lock screen. Specific snippets of my code will be at the bottom of this post.

More than a simple solution for this, though, I'd like to know if there are any additional resources available for learning the kindle libraries. They don't seem to have any javadocs (in the case of the timer, since the API is the same I would assume it would work the same as the java util class of the same name, though my problems would indicate otherwise) and examples are sparse. I do appreciate the number of kindlets posted here which are open sourced on github, but these can be difficult to read through and are typically based around a very event-oriented control flow as far as I can tell.

Thanks for your time,
Andrew

My main class:
Spoiler:
Code:

package klock;

import com.amazon.kindle.kindlet.AbstractKindlet;
import com.amazon.kindle.kindlet.KindletContext;
import com.amazon.kindle.kindlet.ui.KLabel;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
import com.amazon.kindle.kindlet.util.Timer;
import com.amazon.kindle.kindlet.util.TimerTask;

/**
 *
 * @author chronal
 */
public class Main extends AbstractKindlet {

    public KindletContext ctx;
    public KLabel kt;
    public SimpleDateFormat timeFormat;
    public Timer t;
    public TimerTask task;
   
    public void create(KindletContext context) {
        this.ctx = context;
       
        timeFormat = new SimpleDateFormat("HH:mm");
        timeFormat.setTimeZone(TimeZone.getTimeZone("PST"));
       
        try {
            kt = new KLabel("HH:mm", KLabel.CENTER); //timeFormat.format(new Date()), KLabel.CENTER);
            // Alignment 0: Center
            // Alignment 1: ? (Right?)
            // Alignment 2: Nothing?
            kt.setFont(new java.awt.Font(null, 0, 220));
            ctx.getRootContainer().add(kt);
            // Orientation 1: Upside down
            // Orientation 2: Sideways
            ctx.getOrientationController().lockOrientation(2);
           
            t = new Timer();
            t.schedule(
                new UpdateTimeTask(this),
                new Date(),
                15000
            );
           
        }
        catch (Throwable t) {
            t.printStackTrace();
        }
    }
   
   
   
    public void updateTimeText() {

    }
   
   
}

class UpdateTimeTask extends TimerTask
{
    private Main main;
    public UpdateTimeTask(Main main) {
        this.main = main;
    }
   
    public void run() {
        if (main.kt != null) {
            main.kt.setText(main.timeFormat.format(new Date()));
        }
        main.ctx.getRootContainer().repaint(2000);
    }
   
}


Viewing all articles
Browse latest Browse all 4408

Trending Articles