Android 仅在 WiFi 上进行后台下载
Android background download on WiFi only
如果有 Wi-Fi,我的应用程序需要在晚上下载文件。 Service
是最好的实施方式吗?
如果用户关闭了我的应用程序,这是否意味着我无法以任何方式下载?这意味着我的用户必须将我的应用程序保留在后台?
如果你 运行 粘性服务,你可以在工作线程而不是主线程中下载你的文件,即使你的应用是 closed.Therefore 完成后你可以使用广播接收器通知用户或启动 activity.
您可以只使用 DownloadManager
并将允许的网络类型限制为仅 Wi-Fi。
例如:
DownloadManager manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Uri uri = Uri.parse("http://something.xyz/somefile.asd");
DownloadManager.Request request = new DownloadManager.Request(uri);
// allowing Wi-Fi only
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
long id = manager.enqueue(request);
要在下载完成时收到通知,您可以为 ACTION_DOWNLOAD_COMPLETE
广播定义一个接收器:
private final BroadcastReceiver downloadReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// the ID of the finished download
long downloadedId = intent.getLongExtra(
DownloadManager.EXTRA_DOWNLOAD_ID, -1);
// do whatever you'd like here
}
};
和register/unregister它:
@Override
protected void onResume() {
super.onResume();
registerReceiver(downloadReceiver,
new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
@Override
protected void onPause() {
unregisterReceiver(downloadReceiver);
super.onPause();
}
不要忘记将适当的权限添加到您的 Manifest:
<!-- required -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- optional -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
即使您的应用已关闭,您也可以使用服务。我建议使用 IntentService。它易于使用,并且在 UI 线程以外的线程中运行。您也可以将其设置为 STICKY,这样即使 Android 终止服务,您的服务也会重新启动。
如果有 Wi-Fi,我的应用程序需要在晚上下载文件。 Service
是最好的实施方式吗?
如果用户关闭了我的应用程序,这是否意味着我无法以任何方式下载?这意味着我的用户必须将我的应用程序保留在后台?
如果你 运行 粘性服务,你可以在工作线程而不是主线程中下载你的文件,即使你的应用是 closed.Therefore 完成后你可以使用广播接收器通知用户或启动 activity.
您可以只使用 DownloadManager
并将允许的网络类型限制为仅 Wi-Fi。
例如:
DownloadManager manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Uri uri = Uri.parse("http://something.xyz/somefile.asd");
DownloadManager.Request request = new DownloadManager.Request(uri);
// allowing Wi-Fi only
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
long id = manager.enqueue(request);
要在下载完成时收到通知,您可以为 ACTION_DOWNLOAD_COMPLETE
广播定义一个接收器:
private final BroadcastReceiver downloadReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// the ID of the finished download
long downloadedId = intent.getLongExtra(
DownloadManager.EXTRA_DOWNLOAD_ID, -1);
// do whatever you'd like here
}
};
和register/unregister它:
@Override
protected void onResume() {
super.onResume();
registerReceiver(downloadReceiver,
new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
@Override
protected void onPause() {
unregisterReceiver(downloadReceiver);
super.onPause();
}
不要忘记将适当的权限添加到您的 Manifest:
<!-- required -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- optional -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
即使您的应用已关闭,您也可以使用服务。我建议使用 IntentService。它易于使用,并且在 UI 线程以外的线程中运行。您也可以将其设置为 STICKY,这样即使 Android 终止服务,您的服务也会重新启动。