2012-06-14 60 views
1

我想在應用程序中從互聯網上下載視頻。因此,我開始了一個新線程,並在該線程中啓動了一個下載文件的服務。但是,它會在仿真器上引發應用程序未響應對話框,但會在後臺下載該文件。我搜索了很多,但找不到任何解決方案。請幫忙!應用程序未在子線程中響應

我的線程如下:

Thread t = new Thread() 
    { 
     public void run() 
     { 
      Log.i("Test Log", "Starting Service"); 
      Intent intent = new Intent(MainActivity.this, DownloadService.class); 
      startService(intent); 
     } 
    }; 
    t.start(); 

和服務的片段如下:

try 
    { 
     Log.i("Test log", "Entered function"); 

     String RootDir = Environment.getExternalStorageDirectory() + File.separator + "Video"; 
     File RootFile = new File(RootDir); 
     RootFile.mkdir(); 

     URL u = new URL(fileURL); 
     HttpURLConnection c = (HttpURLConnection) u.openConnection(); 
     c.setRequestMethod("GET"); 
     c.setDoOutput(true); 
     c.connect(); 

     long fileSize = c.getContentLength(); 
     Log.e("Test log", "File Size is: " +String.valueOf(fileSize)); 

     String downloadedFile = RootFile + "/Sample.mp4"; 
     Log.i("Test Log", downloadedFile); 
     File df = new File(downloadedFile); 

     if(fileSize == df.length()) 
     { 
      Log.i("Test log", "File exists already! Did not download"); 
      stopSelf(); 
     } 
     else 
     { 
      FileOutputStream f = new FileOutputStream(new File(RootFile, fileName)); 
      InputStream in = c.getInputStream(); 
      byte[] buffer = new byte[1024]; 

      int len1 = 0; 
      while ((len1 = in.read(buffer)) > 0) 
      { 
       f.write(buffer, 0, len1); 
      } 
      f.close(); 

      Log.i("Test log", "Downloaded and file saved as "+downloadedFile); 
      stopSelf(); 
     } 
    } 
    catch (Exception e) 
    { 
     Log.e("Catch exception", "Error is " +e.toString()); 
    } 
} 

編輯:下面 是崩潰日誌:

06-14 17:32:32.638: E/ActivityManager(60): ANR in com.pranav.download 
    06-14 17:32:32.638: E/ActivityManager(60): Reason: Executing service com.pranav.download/.DownloadService 
    06-14 17:32:32.638: E/ActivityManager(60): Load: 0.49/0.21/0.17 
    06-14 17:32:32.638: E/ActivityManager(60): CPU usage from 723800ms to 49ms ago: 
    06-14 17:32:32.638: E/ActivityManager(60): system_server: 5% = 4% user + 1% kernel/ faults: 3616 minor 2 major 
    06-14 17:32:32.638: E/ActivityManager(60): adbd: 1% = 0% user + 1% kernel/faults: 1593 minor 
    06-14 17:32:32.638: E/ActivityManager(60): qemud: 0% = 0% user + 0% kernel 
    06-14 17:32:32.638: E/ActivityManager(60): m.android.phone: 0% = 0% user + 0% kernel/faults: 18 minor 
    06-14 17:32:32.638: E/ActivityManager(60): ndroid.launcher: 0% = 0% user + 0% kernel/faults: 869 minor 
    06-14 17:32:32.638: E/ActivityManager(60): d.process.acore: 0% = 0% user + 0% kernel/faults: 1143 minor 
    06-14 17:32:32.638: E/ActivityManager(60): id.defcontainer: 0% = 0% user + 0% kernel/faults: 64 minor 
    06-14 17:32:32.638: E/ActivityManager(60): events/0: 0% = 0% user + 0% kernel 
    06-14 17:32:32.638: E/ActivityManager(60): com.svox.pico: 0% = 0% user + 0% kernel/faults: 28 minor 
    06-14 17:32:32.638: E/ActivityManager(60): rild: 0% = 0% user + 0% kernel 
    06-14 17:32:32.638: E/ActivityManager(60): logcat: 0% = 0% user + 0% kernel 
    06-14 17:32:32.638: E/ActivityManager(60): ndroid.settings: 0% = 0% user + 0% kernel/faults: 8 minor 
    06-14 17:32:32.638: E/ActivityManager(60): d.process.media: 0% = 0% user + 0% kernel/faults: 9 minor 
    06-14 17:32:32.638: E/ActivityManager(60): roid.alarmclock: 0% = 0% user + 0% kernel/faults: 7 minor 
    06-14 17:32:32.638: E/ActivityManager(60): com.android.mms: 0% = 0% user + 0% kernel/faults: 9 minor 
    06-14 17:32:32.638: E/ActivityManager(60): m.android.email: 0% = 0% user + 0% kernel/faults: 8 minor 
    06-14 17:32:32.638: E/ActivityManager(60): netd: 0% = 0% user + 0% kernel/faults: 4 minor 
    06-14 17:32:32.638: E/ActivityManager(60): ronsoft.openwnn: 0% = 0% user + 0% kernel/faults: 7 minor 
    06-14 17:32:32.638: E/ActivityManager(60): .quicksearchbox: 0% = 0% user + 0% kernel/faults: 8 minor 
    06-14 17:32:32.638: E/ActivityManager(60): android.protips: 0% = 0% user + 0% kernel/faults: 7 minor 
    06-14 17:32:32.638: E/ActivityManager(60): ackageinstaller: 0% = 0% user + 0% kernel/faults: 16 minor 
    06-14 17:32:32.638: E/ActivityManager(60): zygote: 0% = 0% user + 0% kernel/faults: 35 minor 
    06-14 17:32:32.638: E/ActivityManager(60): installd: 0% = 0% user + 0% kernel/faults: 3 minor 
    06-14 17:32:32.638: E/ActivityManager(60): m.android.music: 0% = 0% user + 0% kernel/faults: 7 minor 
    06-14 17:32:32.638: E/ActivityManager(60): +pranav.download: 0% = 0% user + 0% kernel 
    06-14 17:32:32.638: E/ActivityManager(60): +pranav.download: 0% = 0% user + 0% kernel 
    06-14 17:32:32.638: E/ActivityManager(60): TOTAL: 14% = 6% user + 7% kernel + 0% irq + 0% softirq 
+0

你使用'IntentService'嗎? – Luksprog

+0

向我們展示崩潰日誌 –

+0

@Luksprog我截至目前使用服務。也嘗試了IntentService。同樣的錯誤。 – Pranav

回答

0

使用async task handler ...以便主線程不會被中斷,並且下載將被執行在後臺...如果你需要一個example..please讓我知道..
使用這個鏈接Async tasks

+0

我希望下載服務在操作後自行終止。這就是我使用stopSelf並嘗試使用IntentService而不使用AsyncTask的原因。在AsyncTask中可能嗎?或者有沒有辦法在操作完成後終止AsyncTask?我殺了它,因爲我將在應用程序中多次調用相同的服務,並且不想通過創建下載請求來提出錯誤,因爲服務已經在運行。 – Pranav

0

您可能已經開始從另一個線程的服務,但它會在UI線程上運行代碼。

在您的服務中,將代碼下載到方法的AsyncTask例如下載視頻的代碼。

new AsyncTask< Void, Void, Void >() { 
    @Override 
    protected Void doInBackground(final Void... params) 
    { 
     // Your download code 
    } 
} 
+0

我不確定線程​​是否會在它已經運行時再次啓動,因此我想殺掉正在下載的線程,因爲我想在應用程序中多次調用它。因此,我在線程中使用了stopSelf並使用了IntentService。 即使先前調用的另一個AsyncTask活動在後臺運行,是否還會啓動另一個AsyncTask活動?或者,有沒有辦法殺死它,以便它不會在以後造成問題? – Pranav