2013-07-17 57 views
1

我正在嘗試構建一個應用程序,我正在同時播放視頻和圖像。每一個小時左右,應用程序必須從互聯網上下載資料。所以要使用新材料我試圖讓它重新啓動。我已經實現了一個runnable,並將代碼下載並重新啓動到run方法中。問題是應用程序在啓動後立即凍結 -在android中下載

synchronized public void run() { 
    download("http://www.justieltsshaddi.com/pankaj/list.txt", 
      Environment.getExternalStorageDirectory() + "/alpha/list.txt"); 
    File beta = new File(Environment.getExternalStorageDirectory() 
      + "/beta/"); 
    File betalist = new File(beta + "/list.txt"); 
    File alpha = new File(Environment.getExternalStorageDirectory() 
      + "/alpha/"); 
    File alphalist = new File(alpha + "/list.txt"); 
    if (alphalist.lastModified() == betalist.lastModified()) { 
     return; 
    } 
    try { 
     FileReader inAlpha = new FileReader(alphalist); 
     BufferedReader br = new BufferedReader(inAlpha); 
     String s; 
     Toast.makeText(this, "Starting Download...", Toast.LENGTH_SHORT) 
       .show(); 
     while ((s = br.readLine()) != null) { 
      download("http://www.justieltsshaddi.com/pankaj" + "/" + s, 
        Environment.getExternalStorageDirectory() + "/alpha/" 
          + s); 
     } 
     // stop the activity to rename folders 
     Toast.makeText(this, "Download done. Restarting...", 
       Toast.LENGTH_SHORT).show(); 
     Log.d("Pankaj", "Download Done"); 
     Intent intent = getIntent(); 
     overridePendingTransition(0, 0); 
     intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); 
     finish(); 

     Log.d("Pankaj", "MainActivity Killed"); 
     // rename alpha to beta 
     deleteSubFolders(beta.toString()); 
     alpha.renameTo(beta); 
     if (!alpha.exists()) { 
      alpha.mkdir(); 
     } 
     File upper = new File(alpha + "/upper/"); 
     if (!upper.exists()) 
      upper.mkdirs(); 
     File lower = new File(alpha + "/lower/"); 
     if (!lower.exists()) 
      lower.mkdirs(); 
     // restart the activity 
     overridePendingTransition(0, 0); 
     startActivity(intent); 

    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

} 

任何幫助表示讚賞。提前致謝。

回答

0

我會建議你使用Aysnc任務下載的文件

private class DownloadFile extends AsyncTask<String, Integer, String> { 
@Override 
protected String doInBackground(String... sUrl) { 
    try { 

     //do your download 
     while ((s = br.readLine()) != null) { 
     download("http://www.justieltsshaddi.com/pankaj" + "/" + s, 
       Environment.getExternalStorageDirectory() + "/alpha/" 
         + s); 
     } 
    } catch (Exception e) { 
    } 
    return null; 
} 

調用下載任務如下 //執行此當下載必須被解僱 DownloadFile downloadFile =新DownloadFile(); downloadFile.execute(「您要下載的文件的網址」);

可能會考慮在循環中觸發下載任務。

+0

有一個小問題。在進一步處理之前,我需要等待下載完成。 –

+0

你可以調用executeOnExecutor來序列化它,並有你的邏輯 – blganesh101

+0

這就是我所做的。執行不會超出第一行。 –

0

使用服務和告警管理

  Calendar cal = Calendar.getInstance(); 
      cal.add(Calendar.SECOND, 10); 

      Intent intent = new Intent(this, reciever.class); 

      PendingIntent pi = PendingIntent.getBroadcast(context, _id, i, 0); 

      AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
      //for 30 mint 60*60*1000 
      alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 
         60*60*1000, pi); 

在廣播reciever啓動服務

public class reciever extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 

}