我想在應用程序中從互聯網上下載視頻。因此,我開始了一個新線程,並在該線程中啓動了一個下載文件的服務。但是,它會在仿真器上引發應用程序未響應對話框,但會在後臺下載該文件。我搜索了很多,但找不到任何解決方案。請幫忙!應用程序未在子線程中響應
我的線程如下:
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
你使用'IntentService'嗎? – Luksprog
向我們展示崩潰日誌 –
@Luksprog我截至目前使用服務。也嘗試了IntentService。同樣的錯誤。 – Pranav