{"id":5676,"date":"2014-04-05T04:21:13","date_gmt":"2014-04-05T04:21:13","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/05\/android-equivalent-for-setinterval-of-javascript-for-class-collection-of-common-programming-errors\/"},"modified":"2014-04-05T04:21:13","modified_gmt":"2014-04-05T04:21:13","slug":"android-equivalent-for-setinterval-of-javascript-for-class-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/05\/android-equivalent-for-setinterval-of-javascript-for-class-collection-of-common-programming-errors\/","title":{"rendered":"android equivalent for setInterval of javascript for class-Collection of common programming errors"},"content":{"rendered":"<p>I think, you could use my Clock class. It starts\/ stops the timing and shows the time and\/or date in the registered TextView fields.<\/p>\n<pre><code>import java.text.SimpleDateFormat;\nimport java.util.ArrayList;\nimport java.util.Date;\nimport android.os.Handler;\nimport android.widget.TextView;\n\/**\n * The class for creating and refreshing many different fields on different layouts,\n * that can hold actual time and\/or date in different formats \n * The formats should be as in http:\/\/developer.android.com\/reference\/java\/text\/SimpleDateFormat.html. \n * Only present and visible fields are being actualized, so there is no need to clean the clock list after closing an activity\n * \n * Examples of use:\n * \n *      Clock.registerClock((TextView) findViewById(R.id.TimeField), \"HH:mm\");\n *      Clock.registerClock((TextView) findViewById(R.id.DateField), \"d.M.yyyy EEE\");\n *      Clock.start(10000L);\n *\n * @author Petr Gangnus\n *\/\npublic final class Clock {\n    \/**\n     * the handler that works instead of timer and supports UI\n     *\/\n    static private Handler handler = new Handler();\n    \/**\n     * the interval of the time refreshing\n     *\/\n    static private long refreshStep;\n\n    \/**\n     * pairs TextView timer+time\/date format\n     *\/\n    private TextView clockFace;\n    private String format;\n    private Clock(TextView clockFace, String format){\n        this.clockFace=clockFace;\n        this.format=format;\n    }\n    \/\/ here is the list of views containing the visual timers that should be held actual\n    static private ArrayList clocks=new ArrayList();\n    \/**\n     * fills all timer fields by actual time value, according to their formats.\n     *\/\n    static private Runnable mUpdateTimeTask = new Runnable() {\n       public void run() {\n           for(Clock clock:clocks){\n               showActualTimeDate(clock);\n           }\n           handler.postDelayed(this,refreshStep);\n       }\n    };\n\n    \/\/============================================ public members ====================================================================\n    \/**\n     * add a clock to the list of updating clocks\n     * @param clockFace - the place where the time or date will be shown \n     * @param format - the format of the time\/date \n     * @return\n     *\/\n    public static boolean registerClock(TextView clockFace, String format){\n        if (clockFace==null) return false;\n        if(clocks.contains(clockFace)){\n            \/\/ old clockFace\n            clocks.get(clocks.indexOf(clockFace)).format=format;\n        } else {\n            \/\/ new clockFace\n            clocks.add(new Clock(clockFace, format));\n        }\n        return true;\n    }\n    \/**\n     * remove a clock from the updating list\n     * @param clockFace\n     * @return\n     *\/\n    public static boolean unRegisterClock(TextView clockFace){\n        if (clockFace==null) return false;\n        if(clocks.contains(clockFace)){\n            \/\/ found clockFace\n            clocks.remove(clocks.indexOf(clockFace));\n        } else {\n            \/\/ not found clockFace\n            return false;\n        }\n        return true;\n    }\n    \/**\n     * put in the \"place\" the actual date\/time in the appropriate \"format\"\n     * @param place\n     * @param format\n     *\/\n    public static void showActualTimeDate(Clock clock){\n        if (clock.clockFace==null) return;\n        if (clock.clockFace.getVisibility()!=TextView.VISIBLE) return;\n        Date thisDate=new Date();\n        SimpleDateFormat df=new SimpleDateFormat(clock.format);\n        clock.clockFace.setText(df.format(thisDate));\n    }\n    \/**\n     * start the ticking for all clocks\n     * @param step the tick interval\n     *\/\n    public static void start(long step) { \n        refreshStep=step;\n        handler.removeCallbacks(mUpdateTimeTask);\n        handler.postDelayed(mUpdateTimeTask, 0);\n    }\n    \/**\n     * Stopping ticking all clocks (not removing them)\n     * the calling could be put somewhere in onStop\n     *\/\n    public static void stop() { \n        handler.removeCallbacks(mUpdateTimeTask);\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I think, you could use my Clock class. It starts\/ stops the timing and shows the time and\/or date in the registered TextView fields. import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import android.os.Handler; import android.widget.TextView; \/** * The class for creating and refreshing many different fields on different layouts, * that can hold actual time and\/or [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5676","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5676","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5676"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5676\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5676"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5676"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5676"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}