How to show FTP upload progress indicator in async class in android?
I tried a lot of things but didn't get a progress bar. Here is my code, and I am calling this class from another Activity, passing the context of this activity to this class.
package com.scft; import it.sauronsoftware.ftp4j.FTPClient; import it.sauronsoftware.ftp4j.FTPDataTransferListener; import java.io.File; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.os.StrictMode; import android.util.Log; import android.widget.PopupWindow; import android.widget.Toast; public class ftpdownload_class { static final String FTP_HOST= "**************"; /********* FTP USERNAME ***********/ static final String FTP_USER = "*********"; /********* FTP PASSWORD ***********/ static final String FTP_PASS ="*******"; File fileDownload=null; long startTime_ftpDownload=0; long endTime_ftpDownload=0; long downloaded_file=0; long startTime_ftpUpload=0; long endTime_ftpUpload=0; FTPClient client=null; public static File m_fileName; private PopupWindow pwindo; String totaltime_ftpUpload,filesize_ftpUpload,ratevalue_ftpUpload,totaltime_ftpDownload,filesize_ftpDownload,ratevalue_ftp_download; Context mContext=null; public ftpdownload_class( Context c) { mContext=c; } public void ftp_downloadStart() { FTPClient ftp = new FTPClient(); try { if (android.os.Build.VERSION.SDK_INT > 9) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); } ftp.connect(FTP_HOST,158);//158 is the port number //System.out.println(ftp.connect(host)[0]); ftp.login(FTP_USER, FTP_PASS); fileDownload = new File("/sdcard/test.mp3"); fileDownload.createNewFile(); startTime_ftpDownload = System.currentTimeMillis(); ftp.download("test.mp3", fileDownload, new FTPDataTransferListener() { // lenghtOfFile = conection.getContentLength(); public void transferred(int arg0) { // download_btn.setVisibility(View.GONE); //Log.v("log_tag", "This is for transfer"); // Toast.makeText(getBaseContext(), " transferred ..."+arg0 , Toast.LENGTH_SHORT).show(); } public void started() { // TODO Auto-generated method stub // Toast.makeText(getBaseContext(), " Download Started ...", Toast.LENGTH_SHORT).show(); //Log.v("log_tag", "This is for started"); } public void failed() { // download_btn.setVisibility(View.VISIBLE); Toast.makeText(mContext, " failed ...", Toast.LENGTH_SHORT).show(); System.out.println(" failed ..." ); } public void completed() { // download_btn.setVisibility(View.VISIBLE); endTime_ftpDownload = System.currentTimeMillis(); //maybe Toast.makeText(mContext, " Download completed ...", Toast.LENGTH_SHORT).show(); getspeedfor_ftp_download(); //Log.v("log_tag", "This is for completed"); } public void aborted() { // download_btn.setVisibility(View.VISIBLE); Toast.makeText(mContext," transfer aborted,please try again...", Toast.LENGTH_SHORT).show(); //Log.v("log_tag", "This is for aborted"); } }); } catch (Exception e) { e.printStackTrace(); try { ftp.disconnect(true); } catch (Exception e2) { e2.printStackTrace(); } } } long downloaded_file_ftp=0; public void getspeedfor_ftp_download() { downloaded_file_ftp = fileDownload.length(); //Log.d("DownloadManager", "download ended: " + ((endTime - startTime) / 1000) + " secs"); String abc = (((endTime_ftpDownload - startTime_ftpDownload) / 1000) + " secs"); totaltime_ftpDownload = abc; double size = (downloaded_file_ftp/1024); if(size<1000) filesize_ftpDownload=String.valueOf(size).concat("Kb"); else filesize_ftpDownload=String.valueOf(size/1024).concat("Mb"); double rate = (((downloaded_file_ftp / 1024) / ((endTime_ftpDownload - startTime_ftpDownload) / 1000)) * 8); rate = Math.round( rate * 100.0 ) / 100.0; if(rate > 1000) ratevalue_ftp_download = String.valueOf(rate / 1024).concat(" Mbps"); else ratevalue_ftp_download = String.valueOf(rate).concat(" Kbps"); Log.d("DownloadManager", "download speed: "+ratevalue_ftp_download); alertStatus_ftp_download(); } public void alertStatus_ftp_download() { AlertDialog.Builder alertDialogBuilderfor_ftp_download = new AlertDialog.Builder(mContext); // set title alertDialogBuilderfor_ftp_download.setTitle("Ftp Download Speed Status"); // set dialog message alertDialogBuilderfor_ftp_download .setMessage("Download Speed : "+ratevalue_ftp_download+", "+"\n Total File Size :"+filesize_ftpDownload+"\nTotal time taken : "+totaltime_ftpDownload) //.setMessage("Download Speed "+ratevalue_ftp_download) .setCancelable(false) .setPositiveButton("Ok",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { // if this button is clicked, close // current activity dialog.cancel(); } }) .setNegativeButton("Cancel",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { // if this button is clicked, just close // the dialog box and do nothing dialog.cancel(); } }); // create alert dialog AlertDialog alertDialogftp_download = alertDialogBuilderfor_ftp_download.create(); // show it alertDialogftp_download.show(); } }
user120612
source share