如果应用程序随服务销毁,我如何保留下载文件 运行(正在下载)?

how can i keep download file running(downloading) if app destroy with service?

如果应用随服务销毁,我如何保留下载文件运行(正在下载)? 我正在使用此 link 下载视频并在后台下载文件,但问题是应用程序销毁下载已取消

now i want to know how can i keep running download file on if app is destroyed

服务代码

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());
        }
    }

}

activity class

 Intent intent = new Intent(VideolIst.this, DownloadService.class);
        intent.putExtra("url", downloadUri.toString());
        intent.putExtra("filepath", destinationUri.toString());
        this.startService(intent);

also defined in manifest as <service android:enabled="true" android:name="DownloadService" >

见下文 link processattribute AndroidManifest.xml

中的服务

https://developer.android.com/guide/topics/manifest/service-element.html

在清单文件中:

<service
 android:name="com.xyz.XyzService"
 android:enabled="true"
 android:exported="true"
 android:process=":remote">
</service>

使用 android:process=":remote" 定义您的服务。您基本上 运行 您在不同进程 (= VM) 中的服务。

希望对您有所帮助!

通过 returning START_REDELIVER_INTENT 尝试,这样服务将被安排重新启动,最后交付的 Intent 通过 onStartCommand 重新交付。

@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
}

有关服务 return 类型的更多信息,请阅读:https://developer.android.com/reference/android/app/Service.html