gpt4 book ai didi

java - Android:如何从不同的类文件运行异步任务?

转载 作者:IT老高 更新时间:2023-10-28 21:17:46 25 4
gpt4 key购买 nike

当我在一个类文件中使用我的代码时,它可以完美运行:

package com.example.downloadfile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.TextView;

public class DownloadFile extends Activity {

public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private ProgressDialog mProgressDialog;
private static String fileName = "yo.html";
private static String fileURL = "http://example.com/tabletcms/tablets/tablet_content/000002/form/Best%20Form%20Ever/html";

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

setContentView(R.layout.main);
TextView tv = new TextView(this);
tv.setText("This is download file program with asynctask... ");
tv.append("\nYo, this line is appended!");

startDownload();

}

private void startDownload() {
new DownloadFileAsync().execute(fileURL);
}


@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DOWNLOAD_PROGRESS:
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Downloading file..");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(true);
mProgressDialog.show();
return mProgressDialog;
default:
return null;
}
}


class DownloadFileAsync extends AsyncTask<String, String, String> {

@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(DIALOG_DOWNLOAD_PROGRESS);
}

@Override
protected String doInBackground(String... aurl) {

try {
File root = Environment.getExternalStorageDirectory();
URL u = new URL(fileURL);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();

int lenghtOfFile = c.getContentLength();

FileOutputStream f = new FileOutputStream(new File(root + "/download/", fileName));

InputStream in = c.getInputStream();

byte[] buffer = new byte[1024];
int len1 = 0;
long total = 0;

while ((len1 = in.read(buffer)) > 0) {
total += len1; //total = total + len1
publishProgress("" + (int)((total*100)/lenghtOfFile));
f.write(buffer, 0, len1);
}
f.close();
} catch (Exception e) {
Log.d("Downloader", e.getMessage());
}

return null;

}

protected void onProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC",progress[0]);
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}

@Override
protected void onPostExecute(String unused) {
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
}
}
}

我想从另一个类文件运行我的异步任务,我有我的代码:

下载文件.java

package com.example.downloadfile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.TextView;

public class DownloadFile extends Activity {


private static String fileName = "yo.html";
private static String fileURL = "http://example.com/tabletcms/tablets/tablet_content/000002/form/Best%20Form%20Ever/html";

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

setContentView(R.layout.main);
TextView tv = new TextView(this);
tv.setText("This is download file program with asynctask... ");
tv.append("\nYo, this line is appended!");

startDownload();

}

private void startDownload() {
new DownloadFileAsync().execute(fileURL);
}

}

下载FileAsync.java

package com.example.downloadfile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;

class DownloadFileAsync extends AsyncTask<String, String, String> {
public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private ProgressDialog mProgressDialog;

@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(DIALOG_DOWNLOAD_PROGRESS);
}

@Override
protected String doInBackground(String... aurl) {

try {
File root = Environment.getExternalStorageDirectory();
URL u = new URL(fileURL);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();

int lenghtOfFile = c.getContentLength();

FileOutputStream f = new FileOutputStream(new File(root + "/download/", fileName));

InputStream in = c.getInputStream();

byte[] buffer = new byte[1024];
int len1 = 0;
long total = 0;

while ((len1 = in.read(buffer)) > 0) {
total += len1; //total = total + len1
publishProgress("" + (int)((total*100)/lenghtOfFile));
f.write(buffer, 0, len1);
}
f.close();
} catch (Exception e) {
Log.d("Downloader", e.getMessage());
}

return null;

}

protected void onProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC",progress[0]);
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}

@Override
protected void onPostExecute(String unused) {
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
}

@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DOWNLOAD_PROGRESS:
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Downloading file..");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(true);
mProgressDialog.show();
return mProgressDialog;
default:
return null;
}
}
}

我正在使用 eclipse,但在我的 DownloadFile.java 文件中出现错误,有许多带红色下划线的代码....我是 java 和 android 开发新手。

最佳答案

您的代码中的一些更改将使其工作:

public class DownloadFileAsync extends AsyncTask<String, String, String> {
public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private ProgressDialog mProgressDialog;

private Context context;

public DownloadFileAsync(Context context)
{
this.context = context;
mProgressDialog = new ProgressDialog(context);
mProgressDialog.setMessage("Downloading file..");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(true);

}

@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog.show();
}

@Override
protected String doInBackground(String... aurl) {

try {
File root = Environment.getExternalStorageDirectory();
URL u = new URL(aurl[0]);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();

int lenghtOfFile = c.getContentLength();

FileOutputStream f = new FileOutputStream(new File(root + "/download/", aurl[1]));

InputStream in = c.getInputStream();

byte[] buffer = new byte[1024];
int len1 = 0;
long total = 0;

while ((len1 = in.read(buffer)) > 0) {
total += len1; //total = total + len1
publishProgress("" + (int)((total*100)/lenghtOfFile));
f.write(buffer, 0, len1);
}
f.close();
} catch (Exception e) {
Log.d("Downloader", e.getMessage());
}

return null;

}

protected void onProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC",progress[0]);
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}

@Override
protected void onPostExecute(String unused) {
mProgressDialog.dismiss();
}


}

还有 DownloadFile 类:

public class DownloadFile extends Activity {


private static String fileName = "yo.html";
private static String fileURL = "http://mydomain.com/tabletcms/tablets/tablet_content/000002/form/Best%20Form%20Ever/html";

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

setContentView(R.layout.main);
TextView tv = new TextView(this);
tv.setText("This is download file program with asynctask... ");
tv.append("\nYo, this line is appended!");

startDownload();

}

private void startDownload() {
new DownloadFileAsync(this).execute(fileURL,fileName);
}

}

关于java - Android:如何从不同的类文件运行异步任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6119305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com