-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 に答える