我有一项活动从网上下载数据。如果我开始下载过程多次,在下载过程中间应用程序关闭。我正在使用AsyncTask下载数据和Runnable来完成步骤。这是我的代码。在完成任务时我是否必须删除处理程序的回调?如果我在暂停或销毁方法中删除了回调,则会给出强制关闭错误。
mBackgroundHandler = new Handler();
mRunnable = new Runnable()
{
public void run()
{
mCounterProgress = KamaApplication.getStep();
Log.v(TAG, "Progress: " + mCounterProgress);
mRefreshProgress.setProgress(mCounterProgress);
mRefreshProgress.invalidate();
if(KamaApplication.getTotalStep() == 100 && KamaApplication.getStep() == 99)
{
mStop = true;
}
if(!mStop)
{
mBackgroundHandler.postDelayed(this, 1000);
}
}
};
mBackgroundHandler.postDelayed(mRunnable, 1000);
这是我的AsyncTask
private class DownloadFilesTask extends AsyncTask<Void, Integer, Void>
{
protected Void doInBackground(Void... params)
{
try
{
StepsGeneratorInputJsonData stepsGeneratorInputJsonData = new StepsGeneratorInputJsonData();
stepsGeneratorInputJsonData.provider = "Android";
stepsGeneratorInputJsonData.identification = "Vogue";
stepsGeneratorInputJsonData.password = "abcd";
mResult = createAndExecuteStepsGenerator(stepsGeneratorInputJsonData);
JSONObject mNewJSONObject = new JSONObject(mResult);
String mEndDate = mNewJSONObject.getString("endDate");
if(mEndDate.equals("null"))
{
mRefreshSuccess = false;
}
else
{
mRefreshSuccess = true;
}
}
catch (Exception eException)
{
Log.v(TAG, "HotTestGettingError: " + eException.toString());
mRefreshSuccess = false;
}
return null;
}
protected void onProgressUpdate(Integer... progress)
{ }
protected void onPostExecute(Void result)
{
if(mRefreshSuccess)
{
insertData();
}
else
{
Message mMessage = new Message();
mMessage.what = 0;
mHandler.sendMessage(mMessage);
}
mBackgroundHandler.removeCallbacks(mRunnable);
updateDisplay();
}
}
我看到了logcat并得到了这个:
06-25 12:08:28.007: INFO/WindowManager(128): WIN DEATH: Window{408fc1d8 com.mygosoftware.kama/com.mygosoftware.kama.Usage paused=false}
06-25 12:08:28.011: INFO/ActivityManager(128): Process com.mygosoftware.kama (pid 17691) has died.
06-25 12:08:28.078: INFO/ActivityManager(128): Start proc com.sec.android.app.twlauncher for activity com.sec.android.app.twlauncher/.Launcher: pid=17817 uid=10015 gids={3003, 1015, 3002}
06-25 12:08:28.078: INFO/ActivityManager(128): Low Memory: No more background processes.
06-25 12:08:28.148: INFO/Zygote(17817): Zygote: pid 17817 has CALL PRIVILEGED permission, then set capability for CAP_SYS_ADMIN (21)
06-25 12:08:28.312: INFO/ActivityThread(17817): Pub com.sec.android.app.twlauncher.settings: com.sec.android.app.twlauncher.LauncherProvider
06-25 12:08:28.445: INFO/Launcher(17817): onCreate(): product model family:S1 product model : GT-I9000
06-24 16:47:38.269: ERROR/InputDispatcher(128): channel '40823250 com.mygosoftware.kama/com.mygosoftware.kama.Usage (server)' ~ Consumer closed input channel or an error occurred. events=0x8
06-24 16:47:38.269: ERROR/InputDispatcher(128): channel '40823250 com.mygosoftware.kama/com.mygosoftware.kama.Usage (server)' ~ Channel is unrecoverably broken and will be disposed!
06-24 16:47:38.304: INFO/ActivityManager(128): Process com.mygosoftware.kama (pid 7637) has died.
我已经放入了AndroidManifest文件
<activity android:name=".Usage" android:screenOrientation="portrait"
android:clearTaskOnLaunch="true">
并设置
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
也放了
我活动中的onPause()
方法。
此外,我已经提到了这个链接Activity restarts on Force Close。我的问题非常相似。我已经完成了他们在回答中所写的所有内容,但仍然收到了错误。
我该怎么做才能消除此错误?
感谢。
答案 0 :(得分:0)
private void startDownload() {
String url = GlobalVariable.Getstr();
new DownloadFileAsync().execute(url);
}
class DownloadFileAsync extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(DIALOG_DOWNLOAD_PROGRESS);
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DOWNLOAD_PROGRESS:
mProgressDialog = new ProgressDialog(this);
// mProgressDialog.setIcon(R.drawable.icon);
mProgressDialog.setIcon(R.drawable.tab_icon_pitchforkfm);
mProgressDialog.setTitle("Downloading file... "
+ fileName.toString());
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(true);
mProgressDialog.show();
return mProgressDialog;
default:
return null;
}
}
@Override
protected String doInBackground(String... arg0) {
int len1 = 0;
int count;
try {
URL url = new URL(GlobalVariable.Getstr());
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
int lenghtOfFile = c.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
String PATH = Environment.getExternalStorageDirectory()
+ "/download/";
Log.v(LOG_TAG, "PATH: " + PATH);
File file = new File(PATH);
file.mkdirs();
String fileName = "workTest.mp3";
File outputFile = new File(file, fileName);
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
long total = 0;
while ((count = is.read(buffer)) != -1) {
total += count;
publishProgress("" + (int) ((total * 100) / lenghtOfFile));
// fos.write(buffer, 0, len1);
fos.write(buffer, 0, count);
}
fos.close();
is.close();
} catch (IOException e) {
Log.d(LOG_TAG, "Error: " + e);
}
return null;
}
protected void onProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC", progress[0]);
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}
protected void onPostExecute(String unused) {
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
//refreshList();
Toast.makeText(
FindFilesByType.this,
"Downloading of " + fileName
+ " complete.", Toast.LENGTH_LONG).show();
try{
refreshList();
} catch (Exception e) {
Toast.makeText(FindFilesByType.this, "ERROR " + e.toString(),
Toast.LENGTH_LONG).show();
}
上面是我的整个代码..这段代码是从URL下载指定的mp3文件..并将其保存到SDCARD ..并向我显示进度条也取决于下载已完成..希望这对你有用..还在 和问题让我知道快乐编码:) :) Pragna