Arbella Arbella - 3 years ago 122
Android Question

Adding Progress Bar or Progress Dialogue for sending and receiving file

I have implemented a file sharing project. I am able to Send and Receive File but want to add progress bar or progress dialogue but I have no clue where to add progress bar to it.

Receiver Activity

public class ReceiverActivity extends AppCompatActivity {

FileReceiver fileReceiver;
ImageButton ibt;

private final Handler mHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
/* case FileReceiver.CODE :
tvCode.setText((int)msg.obj + "");
break;
*/
case FileReceiver.LISTENING :
Toast.makeText(ReceiverActivity.this,"Listening...",Toast.LENGTH_SHORT).show();
break;

case FileReceiver.CONNECTED:
Toast.makeText(ReceiverActivity.this,"Connected!",Toast.LENGTH_SHORT).show();
break;

case FileReceiver.RECEIVING_FILE :
Toast.makeText(ReceiverActivity.this,"Receiving File!",Toast.LENGTH_SHORT).show();
break;

case FileReceiver.FILE_RECEIVED :
File file = (File) msg.obj;
Toast.makeText(ReceiverActivity.this,file.getName() + " Received!",Toast.LENGTH_SHORT).show();
Toast.makeText(ReceiverActivity.this,"Stored in " + file.getAbsolutePath(),Toast.LENGTH_SHORT).show();
fileReceiver.close();
break;

case FileReceiver.RECEIVE_ERROR :
Toast.makeText(ReceiverActivity.this,"Error occured : " + (String)msg.obj,Toast.LENGTH_SHORT).show();
fileReceiver.close();
break;
}
}
};

public void getFile(View view) {

fileReceiver = new FileReceiver(this,mHandler);

fileReceiver.getFile();

}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receiver);

ibt= (ImageButton) findViewById(R.id.imgbtn);
ibt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(ReceiverActivity.this, Selection_Activity.class);
startActivity(i);
}
});


}
}


Receiver Thread

public class ReceiverThread extends Thread {

private int port;
private Messenger messenger;
private ServerSocket listenerSocket;
private Socket communicationSocket;
private int PKT_SIZE = 60*1024;
ProgressDialog dialog;
public ReceiverThread(int port,Messenger messenger){

//



this.port = port;
this.messenger = messenger;
File folder = new File(Environment.getExternalStorageDirectory() + "/FileSharer/");

folder.mkdirs();
}

@Override
public void run() {

Message message;

try {
listenerSocket = new ServerSocket(port);

message = Message.obtain();
message.what = FileReceiver.CODE;
message.obj = port;
messenger.send(message);

message = Message.obtain();
message.what = FileReceiver.LISTENING;
message.obj = "";
messenger.send(message);

communicationSocket = listenerSocket.accept();

message = Message.obtain();
message.what = FileReceiver.CONNECTED;
message.obj = "";
messenger.send(message);


DataInputStream in = new DataInputStream(communicationSocket.getInputStream());

message = Message.obtain();
message.what = FileReceiver.RECEIVING_FILE;
message.obj = "";
messenger.send(message);

// Read File Name and create Output Stream
String fileName = in.readUTF();

File receiveFile = new File(Environment.getExternalStorageDirectory() + "/FileSharer/" + fileName);

DataOutputStream dout = new DataOutputStream(new FileOutputStream(receiveFile,true));

// Read File Size
long fileSize = in.readLong();

int totalLength = 0;
int length = 0;
byte[] receiveData = new byte[PKT_SIZE];

long startTime = System.currentTimeMillis();

// Get the file data
while( fileSize>0 && ( ( length = in.read( receiveData,0,(int) Math.min(receiveData.length,fileSize) ))!= -1) )
{
dout.write(receiveData, 0, length);

totalLength += length;

fileSize -= length;
}

long stopTime = System.currentTimeMillis();

dout.close();

double time = (stopTime - startTime) / 1000.0;

double speed = (totalLength / time) / 1048576.0;

message = Message.obtain();
message.what = FileReceiver.FILE_RECEIVED;
message.obj = receiveFile;

messenger.send(message);

} catch (Exception e){

e.printStackTrace();

message = Message.obtain();
message.what = FileReceiver.RECEIVE_ERROR;
message.obj = e.toString();

try {
messenger.send(message);
} catch (RemoteException re) {
Log.e("ReceiverThread","Error in sending an error message! Error : " + re.toString());
re.printStackTrace();
}

} finally {

try {

if(communicationSocket!=null)
communicationSocket.close();

if(listenerSocket!=null)
listenerSocket.close();

} catch (IOException ioe) {
Log.e("ReceiverThread","Error in closing sockets. Error : " + ioe.toString());
ioe.printStackTrace();
}
}

}
}


Receiver Service

public class ReceiverService extends Service {

private final String PORT = "PORT";
private final String MESSENGER = "MESSENGER";

private int port;
private Messenger messenger;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

Bundle b = intent.getExtras();

port = (int) b.get(PORT);
messenger = (Messenger) b.get(MESSENGER);

ReceiverThread receiverThread = new ReceiverThread(port,messenger);

receiverThread.start();

return START_REDELIVER_INTENT;

}

@Override
public IBinder onBind(Intent intent) {
return null;
}
}


File Receiver

public class FileReceiver {

private final boolean mDebug = true;

// private final int MIN_PORT_NUMBER = 1024;
// private final int MAX_PORT_NUMBER = 65536;
private final String PORT = "PORT";
private final String MESSENGER = "MESSENGER";

// Constants start from 2001
public static final int CODE = 2001;
public static final int LISTENING = 2002;
public static final int CONNECTED = 2003;
public static final int RECEIVING_FILE = 2004;
public static final int FILE_RECEIVED = 2005;
public static final int RECEIVE_ERROR = 2006;

private Context context;
private Handler mHandler;

private Intent i;

public FileReceiver(Context context, Handler mHandler) {
this.context = context;
this.mHandler = mHandler;
}

/* private boolean isPortAvailable(int port) {

boolean available;

if (port < MIN_PORT_NUMBER || port > MAX_PORT_NUMBER) {
throw new IllegalArgumentException("Invalid port: " + port);
}

ServerSocket ss = null;

try {
ss = new ServerSocket(port);
ss.setReuseAddress(true);
available = true;
} catch (IOException e) {
available = false;
} finally {

if (ss != null) {
try {
ss.close();
} catch (IOException e) {

}
}
}

return available;
}
*/
private int getRandomPort() {
//int port = 1001;
int port = 40500;

//do{
// port = (int) (MIN_PORT_NUMBER + 1 + Math.floor(Math.random()*(MAX_PORT_NUMBER - MIN_PORT_NUMBER-1)));

if(mDebug)
Log.i("FileReceiver","Trying port : " + port);

// }while(!isPortAvailable(port));

return port;


// return port;
}

public void getFile(){

int port = getRandomPort();

if(mDebug)
Log.i("FileReceiver","Port : " + port);

i = new Intent(context,ReceiverService.class);

i.putExtra(PORT,port);
i.putExtra(MESSENGER,new Messenger(mHandler));

context.startService(i);

}

public void close() {
context.stopService(i);
}
}

Answer Source

You can add a simple ProgressDialog box from your activity.

Something like :

// global variable 
ProgressDialog mProgressDialog;

...
...

// inside onCreate
mProgressDialog = new ProgressDialog(ReceiveActivity.this);
mProgressDialog.setCancelable(true);
mProgressDialog.setMessage("Downloading file ..");

...
...

// before file downloads
mProgressDialog.show();

...
...

// when file is downloaded
mProgressDialog.dismiss();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download