-1

就像标题所说的那样。我尝试使用 AsyncTask 下载。下载的时候就卡了。我想也许我在下载之前做的太多了,但没有任何效果。下载开始前大约有 15 秒的延迟。它确实显示了下载进度条,但在等待 15 秒之前不会下载任何内容。这是我的代码:

package com.cydeon.plasmamodz;

import com.stericson.RootTools.RootTools;
import com.stericson.RootTools.exceptions.RootDeniedException;
import com.stericson.RootTools.execution.CommandCapture;

public class Bmod extends Activity {


private class DownloadFile extends AsyncTask<String, Integer, String>{
    @Override
    protected String doInBackground(String... sURL) {
        try{
            URL url = new URL(sURL[0]);
            URLConnection connection = url.openConnection();
            connection.connect();
            //Shows 0-100% progress bar
            int fileLength = connection.getContentLength();

            //Download the file
            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream("/sdcard/plasma/battery/batterymod.zip");

            byte data[] = new byte[1024];
            long total = 0;
            int count;
            while ((count = input.read(data)) != -1) {
                total += count;
                //Publish the Progress
                publishProgress((int) (total * 100/fileLength));
                output.write(data, 0, count);
                }

            output.flush();
            output.close();
            input.close();
    } catch (Exception e) {

    }
    return null;
    }

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

    @Override
    protected void onProgressUpdate(Integer... progress){
        super.onProgressUpdate(progress);
        mProgressDialog.setProgress(progress[0]);
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        mProgressDialog.dismiss();
        Context context = getApplicationContext();
        CharSequence text = "Download complete";
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
    }
}

ProgressDialog mProgressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.battery);
    Button bInstallB = (Button) findViewById(R.id.bInstallBattery);
    Button bReturnB = (Button) findViewById(R.id.bReturnBattery);
    ImageView batteryView = (ImageView) findViewById(R.id.ivBattery);
    Bundle battery;
    battery = getIntent().getExtras();
    final String andy = battery.getString("Andy");
    final String bluebox = battery.getString("BlueBox");
    final String circlepercent = battery.getString("CirclePercent");
    final String circles = battery.getString("Circles");
    final String digital = battery.getString("Digital");
    final String dotted = battery.getString("Dotted");
    final String fullcircle1 = battery.getString("FullCircle1");
    final String fullcircle2 = battery.getString("FullCircle2");
    final String gauge = battery.getString("Gauge");
    final String honeycomb = battery.getString("Honeycomb");
    if (andy != null){
        batteryView.setImageResource(R.drawable.android);
    }if (bluebox != null){
        batteryView.setImageResource(R.drawable.bluebox);
    }if (circlepercent != null){
        batteryView.setImageResource(R.drawable.circlepercent);
    }if (circles != null){
        batteryView.setImageResource(R.drawable.circles);
    }if (digital != null){
        batteryView.setImageResource(R.drawable.digital);
    }if (dotted != null){
        batteryView.setImageResource(R.drawable.dotted);
    }if (fullcircle1 != null){
        batteryView.setImageResource(R.drawable.fullcircle1);
    }if (fullcircle2 != null){
        batteryView.setImageResource(R.drawable.fullcircle2);
    }if (gauge != null){
        batteryView.setImageResource(R.drawable.gauge);
    }if (honeycomb != null){
        batteryView.setImageResource(R.drawable.honeycomb);
    }
    mProgressDialog = new ProgressDialog(Bmod.this);
    mProgressDialog.setIndeterminate(false);
    mProgressDialog.setMax(100);
    mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    bInstallB.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            if (andy != null){
                mProgressDialog.setMessage("Downloading Android Batttery Mod..." );
                DownloadFile downloadFile = new DownloadFile();
                downloadFile.execute("https://dl.dropbox.com/s/623weil5bodvzmq/Andy_Battery.zip");
            }if (bluebox != null){

            }if (circlepercent != null){

            }if (circles != null){

            }if (digital != null){

            }if (dotted != null){
            }if (fullcircle1 != null){
            }if (fullcircle2 != null){
            }if (gauge != null){
            }if (honeycomb != null){
            }

        }
    });

    bReturnB.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            finish();
        }
    });
}   }

那么,如果有人看到导致滞后的原因?

4

1 回答 1

0

如果下载的时间长,放弃使用AsyncTask下载... 使用线程...

AsyncTask 的实现变化

于 2013-03-18T02:41:10.537 回答