如果应用程序被服务破坏,我如何保持下载文件运行(下载)? 我正在使用此链接下载视频并在后台下载文件,但问题是应用程序破坏下载已取消
现在我想知道如果应用程序被破坏,我如何继续运行下载文件
服务代码
public class DownloadService extends Service {
private ThinDownloadManager downloadManager;
private static final int DOWNLOAD_THREAD_POOL_SIZE = 4;
RetryPolicy retryPolicy;
public List<Integer> downloadIds;
@Override
public void onCreate() {
super.onCreate();
downloadManager = new ThinDownloadManager(DOWNLOAD_THREAD_POOL_SIZE);
retryPolicy = new DefaultRetryPolicy(5000, 3, 2f);
downloadIds = new ArrayList<>();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null) {
Uri downloadUri = Uri.parse(intent.getStringExtra("url"));
Uri filepath = Uri.parse(intent.getStringExtra("filepath"));
Log.e("url", downloadUri.toString());
Log.e("filepath", filepath.toString());
startDownload(downloadUri, filepath);
} else {
Toast.makeText(this, "intent is null", Toast.LENGTH_SHORT).show();
}
return START_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void startDownload(final Uri downloadUri, final Uri destinationUri) {
try {
int downloadId1 = (int) System.currentTimeMillis();
final DownloadRequest downloadRequest = new DownloadRequest(downloadUri)
.setRetryPolicy(retryPolicy)
.setDestinationURI(destinationUri).setPriority(DownloadRequest.Priority.HIGH)
.setDownloadContext("Download")
.setStatusListener(new DownloadStatusListenerV1() {
@Override
public void onDownloadComplete(DownloadRequest downloadRequest) {
if (downloadIds != null && downloadIds.size() > 0) {
Log.e("complete", downloadRequest.toString());
Log.e("path", destinationUri + "");
}
}
@Override
public void onDownloadFailed(DownloadRequest downloadRequest, int errorCode, String errorMessage) {
if (downloadIds != null && downloadIds.size() > 0) {
Log.e("fail", downloadRequest.toString() + " " + errorMessage);
}
}
@Override
public void onProgress(DownloadRequest downloadRequest, long totalBytes, long downloadedBytes, int progress) {
if (downloadIds != null && downloadIds.size() > 0) {
Log.e("progress", progress + " ");
}
}
});
if (downloadManager.query(downloadId1) == DownloadManager.STATUS_NOT_FOUND) {
downloadId1 = downloadManager.add(downloadRequest);
downloadIds.add(downloadId1);
}
} catch (Exception e) {
Log.e("catch", e.toString());
}
}
}
活动课
Intent intent = new Intent(VideolIst.this, DownloadService.class);
intent.putExtra("url", downloadUri.toString());
intent.putExtra("filepath", destinationUri.toString());
this.startService(intent);
在清单中也定义为
<service android:enabled="true" android:name="DownloadService" >
尝试通过返回START_REDELIVER_INTENT
,因此将安排服务重新启动,并通过 onStartCommand 重新传递上次交付的 Intent。
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null) {
Uri downloadUri = Uri.parse(intent.getStringExtra("url"));
Uri filepath = Uri.parse(intent.getStringExtra("filepath"));
Log.e("url", downloadUri.toString());
Log.e("filepath", filepath.toString());
startDownload(downloadUri, filepath);
} else {
Toast.makeText(this, "intent is null", Toast.LENGTH_SHORT).show();
}
return START_REDELIVER_INTENT; // change this
}
有关服务返回类型的更多信息,请阅读:https : //developer.android.com/reference/android/app/Service.html
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句