0

私は実行中のダウンロード機能を持っています。しかし、それを実行すると、80% の確率で携帯電話が遅くなり、強制的に閉じられ、1 ~ 2 分の非常に長い時間応答しなくなります。このケースは非常にランダムに発生しました。問題が何であるかを実際に追跡することはできません。ダウンロードが完了すると、デバイスは通常の状態に戻ります。Galaxy S2、Galaxy note、SE xperia Arc S、いくつかのテーブルなど、さまざまなデバイスで試しました。問題は同じままです。コードを改善する方法を教えてもらえますか? 以下は私の既存のコードです:

public void onClickDownload(View view){
        String url = "http://www.mydomain.com./" + fileURL;
        url = url.replaceAll(" ","%20");
        String sourceUrl = url;
        new DownloadFileAsync().execute(sourceUrl);
    }   

public class DownloadFileAsync extends AsyncTask<String, Integer, Void> {
        private boolean run_do_in_background = true;

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

    @Override
    protected Void doInBackground(String... aurl) {
        int count;
        try {
            URL url = new URL(aurl[0]);
            URLConnection conexion = url.openConnection();
            conexion.connect();

            int lengthOfFile = conexion.getContentLength();
            Log.d("ANDRO_ASYNC", "Lenght of file: " + lengthOfFile);

            File folder = new File(Environment.getExternalStorageDirectory() + "/MaxApps");
            boolean success = false;
            if (!folder.exists()) {
                success = folder.mkdirs();
            }
            if (!success) {
            } else {
            }

            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream("/sdcard/MaxApps/" + apkURL);

            byte data[] = new byte[100*1024];

            long total = 0;

            while ((count = input.read(data)) != -1) {
            total += count;             
            int progressPercent = (int) ((total*100)/lengthOfFile);
            if(progressPercent % 5 == 0){  
                publishProgress(progressPercent);
                }
            output.write(data, 0, count);
            }

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

                notificationManager.cancel(Integer.parseInt(ID.toString()));

                Notification MyN = new Notification(); MyN.icon = R.drawable.logo1;
                MyN.tickerText = "Download Failed";
                MyN.number = 1;
                MyN.setLatestEventInfo (getApplicationContext(), apkURL + " Download Failed.", "Please try again", MyPI);
                MyN.flags |= Notification.FLAG_AUTO_CANCEL;
                MyNM.notify(1, MyN);    
                run_do_in_background = false;
            }

        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {          
        notification.contentView.setProgressBar(R.id.pbStatus, 100, progress[0], false);
        notificationManager.notify(Integer.parseInt(ID.toString()), notification);
        }

    @Override
    protected void onPostExecute(Void unused) {
        if(run_do_in_background) {
        notificationManager.cancel(Integer.parseInt(ID.toString()));

        Notification MyN = new Notification(); MyN.icon = R.drawable.logo1;
        MyN.tickerText = "Download Complete";
        MyN.number = 1;
        MyN.setLatestEventInfo (getApplicationContext(), "Download Complete, Click to install.", apkURL, MyPI);
        MyN.flags |= Notification.FLAG_AUTO_CANCEL;
        MyNM.notify(Integer.parseInt(ID.toString()) , MyN);
        }
    }
}
4

1 に答える 1

0

UI の更新に時間がかかっている可能性がありますか? たぶん、次のようにテストして、違いがあるかどうかを確認できます。

@Override
protected void onProgressUpdate(Integer... progress) {          
    Log.d("ANDRO_ASYNC", "Progress: " + progress[0]);
}

ところで、これはあなたが求めていることとは関係ありませんが、進行状況を処理するコードに問題があると思います:

int progressPercent = (int) ((total*100)/lengthOfFile);
if(progressPercent % 5 == 0){  
     publishProgress(progressPercent);
}

それは、正確に 5,10,15,20% などの場合にのみ進行状況を更新します。実際には、以前よりも少なくとも %5 進んだときに進行状況を更新したいと思います。

int previousProgress = 0;
while ((count = input.read(data)) != -1) {
    total += count;             
    int progressPercent = (int) ((total*100)/lengthOfFile);
    if(progressPercent - previousProgress >= 5) {  
        previousProgress = progressPercent;
        publishProgress(progressPercent);
    }
    output.write(data, 0, count);
}
于 2012-07-05T10:19:39.583 に答える