2017-01-02 89 views
1

我有這個應用程序,其中的一個功能將允許您下載視頻。下載部分正在工作,但是我需要在下載完成後立即執行另一項功能。目前,我使用AsyncTask,但每當我嘗試在PostExecute上敬酒時,都不會發生任何事情。我想調用另一個函數來加密,然後在下載完成後刪除原始文件。下載管理器Android:下載完成

順便說一句,加密部分也在工作。我唯一需要的是能夠讓我知道下載是否已經完成的事情。

這是我將從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

我不知道是什麼問題,但喲臍帶繞頸嘗試使用getApplicationContext(取而代之的是getActivity) –

+0

還有就是代碼沒有問題。我只需要知道如何知道下載是否完成。 – VouchMe

回答

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> 

現在你需要保存下載參考ID在sharedpref或數據庫 現在保存此下載參考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

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

     }