NativeScript DownloadManager
一个用于与 Android DownloadManager 交互的小型库。
npm i --save nativescript-downloadmanager

NativeScript DownloadManager

DownloadManager 是一个库,允许在 Android 上使用 Android Download Manager 从 Nativescript 下载文件。为什么不使用 Http.getFile() 呢?主要是因为在我看来,如果你要下载的文件不是 仅仅几KB,它几乎是完全损坏的。

安装

  1. tns plugin add nativescript-downloadmanager

  2. 请确保以下权限已在您的 android manifest 中

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.INTERNET"/>

使用方法

使用方法非常直接:这是一个基于简化版默认 Hello world 应用程序的注释示例(即仅移除了点击计数功能并添加了下载方法)。

TLDR:检查下载方法。

import {Observable} from 'data/observable';
// Import the class.
import {DownloadManager} from 'nativescript-downloadmanager'

export class HelloWorldModel extends Observable {

private _message: string;

constructor() {
super();
}

public download() {
// Instantiate a Download Manager. The way it's done (it uses a BroadcastReceiver),
// it'
s mean to be kept alive during all the application lifetime. But we can kill unsubscribe
let dm = new DownloadManager();
// We download a file, in this example a 10mb test file.
// This is the Most simple version of doing it.
// Aside from that there are optional parameters for. Directory (always inside android/data/yourapp/),
// The file name, and title and description for the notification bar. By default it uses the file name
// as title, and no description.
dm.downloadFile("http://cachefly.cachefly.net/10mb.test", function(result,uri) {
// result is a boolean, if the download was successful, it will return true
console.log(result);
// Uri in file:// format of the downloaded file.
console.log(uri);
// unregisterBroadcast is used to unregister the broadcast (For example if you just want to
// download a single file).
dm.unregisterBroadcast();
})
}
}

待办事项

  • 进行更多测试。
  • 更好地记录文档。
  • 制作某种 iOS 模拟版本。