我想在我的应用程序中从互联网上下载视频。因此,我开始了一个新线程,在该线程中我启动了一个下载文件的服务。但是,它会在模拟器上抛出应用程序未响应对话框,但会在后台下载文件。我搜索了很多但找不到任何解决方案。请帮忙!
我的主题如下:
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 :(得分:0)
使用async task handler
...这样主线程就不会被中断,下载会在后台进行......如果你需要一个例子......请告诉我..
使用此链接Async tasks
答案 1 :(得分:0)
您可能已经从另一个线程启动了该服务,但它将在UI线程上运行代码。
在您的服务中,请使用doInBackground(...)
AsyncTask
方法下载视频代码。
new AsyncTask< Void, Void, Void >() {
@Override
protected Void doInBackground( final Void... params )
{
// Your download code
}
}