Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.app.Service;
- import android.content.Intent;
- import android.os.Binder;
- import android.os.IBinder;
- import android.support.v4.content.LocalBroadcastManager;
- import android.util.Log;
- import android.widget.Toast;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import it.sauronsoftware.ftp4j.FTPAbortedException;
- import it.sauronsoftware.ftp4j.FTPClient;
- import it.sauronsoftware.ftp4j.FTPDataTransferException;
- import it.sauronsoftware.ftp4j.FTPDataTransferListener;
- import it.sauronsoftware.ftp4j.FTPException;
- import it.sauronsoftware.ftp4j.FTPIllegalReplyException;
- /**
- * Created by Varsha M on 9/20/2016.
- */
- public class UploadService extends Service {
- private static final String FTP_HOST = "192.168.2.99";
- private static final String FTP_USER = "Administrator";
- private static final String FTP_PASS = "windows_10";
- private int FTP_PORT = 21;
- private boolean isStopped = false;
- private long fileSize;
- private int overAllPercentage;
- Thread thread = new Thread(new Runnable() {
- @Override
- public void run() {
- uploadData();
- }
- });
- @Override
- public void onCreate() {
- overAllPercentage = 0;
- thread.start();
- super.onCreate();
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- return START_STICKY;
- }
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- //returns the instance of the service
- /* public class LocalBinder extends Binder {
- public UploadService getServiceInstance() {
- return UploadService.this;
- }
- }*/
- private void uploadData() {
- FTPClient client = new FTPClient();
- try {
- client.connect(FTP_HOST, FTP_PORT);
- client.login(FTP_USER, FTP_PASS);
- client.setType(FTPClient.TYPE_BINARY);
- client.changeDirectory("/Varsha");
- if (isStopped)
- return;
- File file = new File("/storage/emulated/0/Download/Professional_Android_Application_Development.pdf");
- fileSize = file.length();
- client.upload(file, new MyTransferListener());
- client.disconnect(true);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (FTPException e) {
- e.printStackTrace();
- } catch (FTPDataTransferException e) {
- e.printStackTrace();
- } catch (FTPAbortedException e) {
- e.printStackTrace();
- } catch (FTPIllegalReplyException e) {
- e.printStackTrace();
- }
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- Log.e("CC","onDestroy");
- }
- private class MyTransferListener implements FTPDataTransferListener {
- int max;
- int count = 0;
- @Override
- public void started() {
- Log.e("CC", "started");
- max = (int) fileSize;
- }
- @Override
- public void transferred(int i) {
- count += i;
- int percent = count * 100 / max;
- sendUploadStatusBroadcast((overAllPercentage + (percent)));
- Log.e("CC", "transferring");
- if (percent == 100) {
- overAllPercentage += (percent);
- }
- }
- @Override
- public void completed() {
- isStopped = true;
- Log.e("CC", "compleated");
- stopSelf();
- }
- @Override
- public void aborted() {
- Log.e("CC","aborted");
- }
- @Override
- public void failed() {
- Log.e("CC","failed");
- }
- }
- private void sendUploadStatusBroadcast(int progress) {
- Intent uploadProgressIntent = new Intent(getString(R.string.status));
- uploadProgressIntent.putExtra("PROGRESS", progress);
- LocalBroadcastManager.getInstance(this).sendBroadcast(uploadProgressIntent);
- }
- }
Add Comment
Please, Sign In to add comment