{"id":398,"date":"2023-02-25T08:53:23","date_gmt":"2023-02-25T00:53:23","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=398"},"modified":"2023-04-29T21:38:16","modified_gmt":"2023-04-29T13:38:16","slug":"rxjava2-learning-10-countdown-to-sending-verification-codes","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/rxjava2-learning-10-countdown-to-sending-verification-codes\/","title":{"rendered":"RxJava2\u5b66\u4e60\u4e4b\u5341\uff1a\u53d1\u9001\u9a8c\u8bc1\u7801\u5012\u8ba1\u65f6"},"content":{"rendered":"<h2>CountDownTimer\u5b9e\u73b0<\/h2>\n<p>Android\u63d0\u4f9b\u4e00\u4e2aCountdownTimer\u7684\u7c7b\uff0c\u91c7\u7528Handler\u7684\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5b9e\u73b0\u5012\u6570\u8ba1\u65f6\u7684\u529f\u80fd\u3002<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">\/\/\u7b2c\u4e00\u4e2a\u53c2\u6570\u8868\u793a\u603b\u65f6\u95f4\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u8868\u793a\u95f4\u9694\u65f6\u95f4\u3002\u5355\u4f4dms\uff0c\u610f\u601d\u5c31\u662f\u6bcf\u9694100ms\u4f1a\u56de\u8c03\u4e00\u6b21\u65b9\u6cd5onTick\nnew CountDownTimer(1000, 100) {\n    @Override\n    public void onTick(long l) {\n        Log.i(TAG, &quot;onTick: &quot; + l);\n    }\n\n    @Override\n    public void onFinish() {\n        Log.i(TAG, &quot;onFinish: &quot;);\n    }\n}.start();<\/code><\/pre>\n<h2>RxJava\u5b9e\u73b0<\/h2>\n<h4>RxJava\u64cd\u4f5c\u7b26<\/h4>\n<p>\u672c\u4f8b\u6d89\u53ca\u7684RxJava\u64cd\u4f5c\u7b26\uff1a<\/p>\n<ul>\n<li><strong>Interval<\/strong> \u2014 <em>\u521b\u5efa\u4e00\u4e2a\u5b9a\u65f6\u53d1\u5c04\u6574\u6570\u5e8f\u5217\u7684Observable<\/em><\/li>\n<\/ul>\n<h4>RxJava\u5b9e\u73b0<\/h4>\n<pre><code class=\"language-java\">public void sendVerificationCode(View view) {\n    final Button button = (Button) this.findViewById(R.id.send_verification_code);\n    final long count = 10;\n    Observable.interval(0, 1, TimeUnit.SECONDS)\n        .take(count+1)\n        .map(new Function&lt;Long, Long&gt;() {\n            @Override\n            public Long apply(Long aLong) throws Exception {\n                return count - aLong;\n            }\n        })\n        .observeOn(AndroidSchedulers.mainThread())\n        .doOnSubscribe(new Consumer&lt;Disposable&gt;() {  \/\/\u521d\u59cb\u72b6\u6001\n            @Override\n            public void accept(Disposable disposable) throws Exception {\n                button.setBackgroundColor(Color.parseColor(&quot;#A5A5A5&quot;));\n                button.setEnabled(false);\n            }\n        })\n        .subscribe(new Observer&lt;Long&gt;() {\n            @Override\n            public void onSubscribe(Disposable d) {\n\n            }\n\n            @Override\n            public void onNext(Long value) {\n                Log.i(TAG, &quot;onNext: &quot; + value);\n                button.setText(&quot;\u5269\u4f59 &quot; + value + &quot; \u79d2&quot;);\n            }\n\n            @Override\n            public void onError(Throwable e) {\n                e.printStackTrace();\n            }\n\n            @Override\n            public void onComplete() {\n                Log.i(TAG, &quot;onComplete: &quot;);\n                button.setEnabled(true);\n                button.setBackgroundColor(Color.parseColor(&quot;#3BC1FF&quot;));\n                button.setText(&quot;\u53d1\u9001\u9a8c\u8bc1\u7801&quot;);\n            }\n        });\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>CountDownTimer\u5b9e\u73b0 Android\u63d0\u4f9b\u4e00\u4e2aCountdownTimer\u7684\u7c7b\uff0c\u91c7\u7528Handler\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[131],"tags":[132],"class_list":["post-398","post","type-post","status-publish","format-standard","hentry","category-rxjava","tag-rxjava"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/398","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=398"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/398\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}