3

用戶可以添加任意數量的下載,但下載將會一個接一個地開始,即下一個下載將在完成當前下載後纔會啓動。用戶將離開顯示下載進度的活動,以便添加新的下載,並且當添加新的要下載的文件時,應用程序返回到顯示下載進度的活動,其中它將顯示下載的進度,之前添加,並保持當前添加的文件爲待處理下載。一旦下載完成,掛起的下載將開始下載。 用這種方式,用戶可以添加任意數量的下載,並且他們將一個接一個地開始。我想在後臺連續下載它們 - 一個接一個。我想在ListView中顯示進度和狀態。因此,ListView的樣子:Android一個接一個地下載多個文件並在ListView中顯示進度

文件1 ...進展說39%

文件2 ....待定

文件3 ...待定

FILE4 ......待

+0

考慮使用的AsyncTask借來的,但不知道如何表達下載完成後,開始爲下一個待處理的下載進度條。另外,如果用戶導航到其他活動以添加新下載,如何保持進度欄更新。 – user2416657

回答

1

我建議使用IntentServices:

public class FileDownloader extends IntentService { 

private static final String TAG = FileDownloader.class.getName(); 



public FileDownloader() { 
    super("FileDownloader"); 
} 

@Override 
protected void onHandleIntent(Intent intent) { 
    String fileName = intent.getStringExtra("Filename"); 
    String folderPath = intent.getStringExtra("Path"); 
    String callBackIntent = intent 
      .getStringExtra("CallbackString"); 

    // Code for downloading 

    // When you want to update progress call the sendCallback method 

} 

private void sendCallback(String CallbackString, String path, 
     int progress) { 

     Intent i = new Intent(callBackIntent); 
     i.putExtra("Filepath", path); 
     i.putExtra("Progress", progress); 
     sendBroadcast(i); 

} 

} 

然後開始下載文件,簡單地做:

Intent i = new Intent(context, FileDownloader.class); 
i.putExtra("Path", folderpath); 
i.putExtra("Filename", filename); 
i.putExtra("CallbackString", 
      "progress_callback"); 
startService(i); 

現在你應該處理「progress_callback」回調,你會與任何其他廣播,註冊接收器等。在這個例子中,使用的文件路徑,以確定哪些文件應該有它的進步視覺更新。

不要忘記在清單中註冊服務。

<service android:name="yourpackage.FileDownloader" /> 

注:

有了這個解決方案,您可以立即開始爲每個文件服務和隨便處理傳入的廣播,因爲每個服務報告新的進展。在開始下一個之前,不需要等待每個文件被下載。但是,如果你堅持以串行方式下載文件,你當然可以在調用下一個之前等待100%進度回調。

使用 'CallbackString'

您可以使用它像這樣在你的活動:

private BroadcastReceiver receiver; 

@Overrride 
public void onCreate(Bundle savedInstanceState){ 

    // your oncreate code 

    // starting the download service 

    Intent i = new Intent(context, FileDownloader.class); 
    i.putExtra("Path", folderpath); 
    i.putExtra("Filename", filename); 
    i.putExtra("CallbackString", 
      "progress_callback"); 
    startService(i); 

    // register a receiver for callbacks 
    IntentFilter filter = new IntentFilter("progress_callback"); 

    receiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     //do something based on the intent's action 
     Bundle b = intent.getExtras(); 
     String filepath = b.getString("Filepath"); 
     int progress = b.getInt("Progress"); 
     // could be used to update a progress bar or show info somewhere in the Activity 
    } 
    } 
    registerReceiver(receiver, filter); 
} 

記得在onDestroy方法運行此:

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    unregisterReceiver(receiver); 
} 

注意「 progress_callback「可以是您選擇的任何其他字符串。

示例代碼Programmatically register a broadcast receiver

+0

你能否提供一些關於CallbackString的更多信息?究竟是什麼,我該如何使用它? –

+0

@piavgh試圖更新答案 – cYrixmorten

相關問題