{"id":221066,"date":"2020-11-22T22:48:19","date_gmt":"2020-11-22T14:48:19","guid":{"rendered":"http:\/\/4563.org\/?p=221066"},"modified":"2020-11-22T22:48:19","modified_gmt":"2020-11-22T14:48:19","slug":"android-kotlin-%e8%af%b7%e9%97%ae%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0-javascript-%e4%b8%ad%e7%9a%84-promise-%e5%bc%82%e6%ad%a5%e9%93%be%e5%bc%8f%e6%b5%81%e7%a8%8b","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=221066","title":{"rendered":"android\/Kotlin: \u8bf7\u95ee\u5982\u4f55\u5b9e\u73b0 JavaScript \u4e2d\u7684 promise \u5f02\u6b65\u94fe\u5f0f\u6d41\u7a0b?"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  android\/Kotlin: \u8bf7\u95ee\u5982\u4f55\u5b9e\u73b0 JavaScript \u4e2d\u7684 promise \u5f02\u6b65\u94fe\u5f0f\u6d41\u7a0b?               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : sprinter <\/span>  <span><i><\/i> 4<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u6211\u6709\u4e00\u4e2a\u4e0b\u8f7d object \u7c7b DownloadManager, \u4f5c\u7528\u662f\u4e0b\u8f7d\u6587\u4ef6, \u89c1\u5982\u4e0b\u4ee3\u7801\u6240\u793a.<\/p>\n<p>\u6211\u5148\u8c03\u7528\u8fd9\u4e2a\u7c7b\u7684 beginDownload \u65b9\u6cd5, \u5f00\u59cb\u4e0b\u8f7d\u6587\u4ef6, \u6587\u4ef6\u4e0b\u8f7d\u5b8c\u6210\u540e\u8be5\u7c7b\u4f1a\u901a\u8fc7\u5e7f\u64ad BroadcastReceiver \u53d1\u9001\u4e0b\u8f7d\u6210\u529f\u7684\u6d88\u606f.<\/p>\n<p>object DownloadManager {<\/p>\n<pre><code>var downloadID: Long = 0  fun beginDownload(context: Context, url: String): Long {      val file = File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), File(url).name)      val request =          DownloadManager.Request(Uri.parse(url))              .setDestinationUri(Uri.fromFile(file)) \/\/ Uri of the destination file              .setAllowedOverMetered(true) \/\/  Allow download on Mobile network      val dm = context.getSystemService(DOWNLOAD_SERVICE) as DownloadManager      downloadID = dm.enqueue(request) \/\/ enqueue the download request.      return downloadID  }    val br: BroadcastReceiver = object : BroadcastReceiver() {      override fun onReceive(context: Context, intent: Intent) {          val id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1)          if (downloadID.toLong() === id) { <\/code><\/pre>\n<p>\/\/\u4e0b\u8f7d\u6210\u529f Toast.makeText(context, &#8220;Download complete&#8221;, Toast.LENGTH_SHORT).show()<\/p>\n<pre><code>        }  }  }} <\/code><\/pre>\n<p>\u6211\u7684\u95ee\u9898:<\/p>\n<p>\u6211\u4ee5\u524d\u7528 JavaScript \u65f6\u4e60\u60ef\u7528 promise \u6765\u6839\u636e\u4e0b\u8f7d\u6210\u529f\u4e0e\u5426\u6267\u884c\u4e0b\u9762\u52a8\u4f5c, \u4ee3\u7801\u5982\u4e0b:<\/p>\n<p>new Promise(function(resolve, reject) {<\/p>\n<p>\u7528 DownloadManager \u7c7b \u4e0b\u8f7d\u7b2c\u4e00\u4e2a\u6587\u4ef6<\/p>\n<p>}).then(function(result) {<\/p>\n<p>\u7528 DownloadManager \u7c7b \u4e0b\u8f7d\u7b2c\u4e00\u4e2a\u6587\u4ef6<\/p>\n<p>}).then(function(result) {<\/p>\n<p>\u7528 DownloadManager \u7c7b \u4e0b\u8f7d\u7b2c\u4e00\u4e2a\u6587\u4ef6<\/p>\n<p>})<\/p>\n<p>\u8bf7\u95ee\u4e0a\u8ff0\u94fe\u5f0f\u7684 promise \u7ed3\u6784\u5728 android kotlin \u91cc\u5982\u4f55\u5199?<\/p>\n<p>\u8c22\u8c22\u6307\u6559! \u5c0f\u5973\u611f\u6fc0\u4e0d\u5c3d<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>6<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4451946\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : karnaugh <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             RxAndroid \uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4451947\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xFrye <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u53ef\u4ee5\u770b\u4e0b kotlin \u7684 flow                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4451948\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : luwies <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             RxJava \u5e94\u8be5\u662f\u53ef\u4ee5\u505a\u5230                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4451949\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : kazeik <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             rxjava \u53ef\u4ee5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4451950\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : winterbells <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7528\u534f\u7a0b\u5427\uff0c\u53ef\u4ee5\u628a\u6240\u6709 then \u884c\u5220\u6389\uff0c\u540c\u6b65\u7684\u5199\u6cd5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4451951\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : mxalbert1996 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u559c\u6b22 Promise \u7684\u8bed\u6cd5\u5c31\u7528 RxJava\uff0c\u559c\u6b22 async\/await \u7684\u8bed\u6cd5\u5c31\u7528 coroutines                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>android\/Kotlin: \u8bf7&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/221066"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=221066"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/221066\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=221066"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=221066"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=221066"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}