アプリケーションでインターネットからビデオをダウンロードしたいと考えています。したがって、私は新しいスレッドを開始し、そのスレッドでファイルをダウンロードするサービスを開始しました。ただし、エミュレーターでアプリケーションが応答しないというダイアログがスローされますが、バックグラウンドでファイルがダウンロードされます。私はたくさん検索しましたが、解決策が見つかりませんでした。助けてください!
私のスレッドは以下の通りです:
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