我有这个应用程序,其中的一个功能将允许您下载视频。下载部分正在运行,但我需要在下载完成后立即执行其他功能。目前,我正在使用AsyncTask,但每当我尝试在PostExecute上进行干杯时,都没有任何反应。我想调用另一个函数进行加密,然后在下载完成后删除原始文件。
顺便说一句,加密部分也在运行。我唯一需要的是能让我知道下载是否已经完成的事情。
这是我将从URL下载文件的代码。但是,我需要知道下载是否完成以执行AsyncTask
public void downloadTutorial() throws Exception {
myURL = "";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(myURL));
request.setTitle(injuryType + " Video");
request.setDescription("File is being downloaded...");
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
String fileName = URLUtil.guessFileName(myURL, null, MimeTypeMap.getFileExtensionFromUrl(myURL));
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
DownloadManager manager = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
//if the download is complete execute this
//new JSONTask().execute();
}
AsyncTask的代码是:
public class JSONTask extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... params) {
try {
Encrypter.encrypt(injuryType);
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Toast.makeText(getActivity(), "Download Done", Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:1)
因此,您希望在使用“下载管理器”下载文件后访问该文件。 首先,您将需要一个广播接收器,它将在下载文件后通知您。
在清单中:
<receiver
android:name="<your download receiver class extends Broadcast receiver>"
android:enabled="true"
android:protectionLevel= "signature"
>
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
</intent-filter>
</receiver>
现在您需要在sharedpref或database中保存下载引用ID 现在将此下载引用ID保存在您的sharedpref或数据库中,以便我们可以在广播接收器中获取它。
Uri uri = Uri.parse(content.url);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setDescription("Your App Title").setTitle(<file title>);
request.setDestinationInExternalFilesDir(getActivity(), Environment.DIRECTORY_DOWNLOADS, <file title>);
request.setVisibleInDownloadsUi(false); //the content will not shown in Download Manager App
mydownlodreference = downloadManager.enqueue(request);
现在主要部分,在onReceive的BroadcastReceiver类
@Override
public void onReceive(Context context, Intent intent) {
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
long reference = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
// take out the reference id from your sharedpref or database and check that referenceId with this reference
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(reference);
Cursor c = downloadManager.query(query);
c.moveToFirst();
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
int status = c.getInt(columnIndex);
int fileNameIndex = c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME);
String filePath = c.getString(fileNameIndex);
if (status == DownloadManager.STATUS_SUCCESSFUL) {
// do whatever you want here
}
}