2016-02-09 2 views
0

Это код, который я сейчас используюКак я могу подождать, пока DownloadManager не будет завершен?

   String iosjiUrl = "http://modapps.com/NotoCoji.ttf"; 
       DownloadManager.Request request = new DownloadManager.Request(Uri.parse(iosjiUrl)); 
       request.setDescription("Sscrition"); 
       request.setTitle("Somle"); 
// in order for this if to run, you must use the android 3.2 to compile your app 
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
        request.allowScanningByMediaScanner(); 
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 
       } 
       request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "NotoColorji.ttf"); 

// get download service and enqueue file 
       DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); 

       manager.enqueue(request); 

Я не смог найти способ для меня, чтобы ждать, пока файл не будет загружен.

Я также попытался выяснить, как идти об этом ASync, но не мог понять, что из либо =/

Еще раз спасибо!

+0

вы можете использовать после выполнения в asyntask – geniushkg

+0

Попробуйте эту статью http://blog.vogella.com/2011/06/14/android-downloadmanager-example/. – TdSoft

ответ

3

Используйте BroadcastReceiver, чтобы обнаружить, когда загрузка заканчивается:

public class DownloadBroadcastReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     String action = intent.getAction(); 

     if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) { 
      //Show a notification 
     } 
    } 
} 

и зарегистрировать его в манифесте:

<receiver android:name=".DownloadBroadcastReceiver"> 
    <intent-filter> 
     <action android:name="android.intent.action.DOWNLOAD_COMPLETE"/> 
    </intent-filter> 
</receiver> 
+0

Где именно я помещаю этот первый блок кода? – IdkHowToCodeAtAll

+1

Это широковещательный приемник. Вам нужно будет создать новый класс и сохранить его как 'DownloadBroadcastReceiver.java' – camelCaseCoder

+0

Хорошо. Мне жаль, что я похож на супер отсталый от Android Apps и Android Studio. В какой директории я должен включить первый блок кода? А где в манифесте я должен поставить второй блок кода? – IdkHowToCodeAtAll

1

A Broadcast intent action sent by the download manager when a download completes so you need to register a receiver for when the download is complete: 
 

 
To register receiver 
 

 
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); 
 

 
and a BroadcastReciever handler 
 

 
BroadcastReceiver onComplete=new BroadcastReceiver() { 
 
    public void onReceive(Context ctxt, Intent intent) { 
 
     // your code 
 
    } 
 
}; 
 

 
You can also create AsyncTask to handle the downloading of big files 
 

 
Create a download dialog of some sort to display downloading in notification area and than handle the opening of the file: 
 

 
protected void openFile(String fileName) { 
 
    Intent install = new Intent(Intent.ACTION_VIEW); 
 
    install.setDataAndType(Uri.fromFile(new File(fileName)),"MIME-TYPE"); 
 
    startActivity(install); 
 
} 
 

 
you can also check the sample link
sample