发件人和收件人的进度条不同步

时间:2014-08-12 08:30:58

标签: sockets file-io android-asynctask progress-bar android-notifications

我在处理Android ProgressBar时遇到了一个奇怪的问题。我已经编写了一个基于套接字的文件传输应用程序。文件传输工作正常,服务器端的进度条也与传输的文件大小同步。但是,在接收方结束时,进度条很快达到100%(从服务器传输文件仍在进行中)。任何想法可能是什么原因?以下是使用AsyncTask的接收方实施:

private class ConnectToServerTask extends AsyncTask<Void, Integer, Void> {
    NotificationManager mNotifyMgr;
    NotificationCompat.Builder mBuilder;
    int mNotificationId;

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

        // Build notification object

        mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        mBuilder= new NotificationCompat.Builder(getBaseContext())
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("Airwire")
        .setContentText("Copy in progress")
        .setAutoCancel(true);
        mNotificationId = 001;
    }

    @Override
    protected Void doInBackground(Void... params) {
        final EditText ipText=(EditText)findViewById(R.id.ipText);
        try {
            serverIP=InetAddress.getByName(ipText.getText().toString());
            socket = new Socket(serverIP, 4444);
            if (socket != null) {
                InputStream min=socket.getInputStream();
                int filecount=min.read(); //read file count from server
                for (int j = 0; j < filecount; j++){
                    try {
                        InputStream in = socket.getInputStream();
                        int buffersize = socket.getReceiveBufferSize();
                        DataInputStream clientData = new DataInputStream(in);
                        String recFileName = clientData.readUTF(); // read file name
                        FileOutputStream output = new FileOutputStream(
                                downloadFolderPath +"/" + recFileName);
                        byte[] buffer = new byte[buffersize];
                        long filesize = clientData.readLong(); // read file size
                        double nosofpackets=Math.ceil(filesize/buffer.length);
                        long z = filesize;
                        int n = 0;

                        int k=1;

                        while ((z > 0)
                                && (n = clientData.read(buffer, 0,
                                        (int) Math.min(buffer.length, z))) != -1) {
                            output.write(buffer, 0, n);
                            int progressint=(int)(k*100/nosofpackets);
                            publishProgress(Math.min(progressint, 100));
                            Log.d("PROGRESSINT", progressint+"");
                            output.flush();
                            z -= n;
                            k++;
                        }
                        output.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
    }

    @Override
    protected void onProgressUpdate(Integer...values) {
        super.onProgressUpdate(values);

        //update notification bar
        mBuilder.setContentText("Copy in progress: "+values[0]+"%");
        if (values[0]%2==0){
            mBuilder.setProgress(100, values[0], false);
            mNotifyMgr.notify(mNotificationId, mBuilder.build());
        }
    }       
}

我正在preExecute()中构建通知对象(带进度条)。为避免频繁更新通知,我只会通知2中的多个进度。请参阅progressint中用于计算doInBackground()的方法。这是计算文件传输进度的正确方法吗?任何帮助将非常感谢!

0 个答案:

没有答案