Guest User

Untitled

a guest
Oct 6th, 2016
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1.  
  2. import android.app.Service;
  3. import android.content.Intent;
  4. import android.os.Binder;
  5. import android.os.IBinder;
  6. import android.support.v4.content.LocalBroadcastManager;
  7. import android.util.Log;
  8. import android.widget.Toast;
  9.  
  10. import java.io.File;
  11. import java.io.FileNotFoundException;
  12. import java.io.IOException;
  13.  
  14. import it.sauronsoftware.ftp4j.FTPAbortedException;
  15. import it.sauronsoftware.ftp4j.FTPClient;
  16. import it.sauronsoftware.ftp4j.FTPDataTransferException;
  17. import it.sauronsoftware.ftp4j.FTPDataTransferListener;
  18. import it.sauronsoftware.ftp4j.FTPException;
  19. import it.sauronsoftware.ftp4j.FTPIllegalReplyException;
  20.  
  21. /**
  22. * Created by Varsha M on 9/20/2016.
  23. */
  24. public class UploadService extends Service {
  25. private static final String FTP_HOST = "192.168.2.99";
  26. private static final String FTP_USER = "Administrator";
  27. private static final String FTP_PASS = "windows_10";
  28. private int FTP_PORT = 21;
  29. private boolean isStopped = false;
  30. private long fileSize;
  31. private int overAllPercentage;
  32.  
  33. Thread thread = new Thread(new Runnable() {
  34. @Override
  35. public void run() {
  36. uploadData();
  37. }
  38. });
  39.  
  40. @Override
  41. public void onCreate() {
  42. overAllPercentage = 0;
  43. thread.start();
  44. super.onCreate();
  45. }
  46.  
  47. @Override
  48. public int onStartCommand(Intent intent, int flags, int startId) {
  49. return START_STICKY;
  50. }
  51.  
  52. @Override
  53. public IBinder onBind(Intent intent) {
  54. return null;
  55. }
  56.  
  57. //returns the instance of the service
  58. /* public class LocalBinder extends Binder {
  59. public UploadService getServiceInstance() {
  60. return UploadService.this;
  61. }
  62. }*/
  63.  
  64. private void uploadData() {
  65. FTPClient client = new FTPClient();
  66. try {
  67. client.connect(FTP_HOST, FTP_PORT);
  68. client.login(FTP_USER, FTP_PASS);
  69. client.setType(FTPClient.TYPE_BINARY);
  70. client.changeDirectory("/Varsha");
  71. if (isStopped)
  72. return;
  73. File file = new File("/storage/emulated/0/Download/Professional_Android_Application_Development.pdf");
  74. fileSize = file.length();
  75. client.upload(file, new MyTransferListener());
  76. client.disconnect(true);
  77. } catch (FileNotFoundException e) {
  78. e.printStackTrace();
  79. } catch (IOException e) {
  80. e.printStackTrace();
  81. } catch (FTPException e) {
  82. e.printStackTrace();
  83. } catch (FTPDataTransferException e) {
  84. e.printStackTrace();
  85. } catch (FTPAbortedException e) {
  86. e.printStackTrace();
  87. } catch (FTPIllegalReplyException e) {
  88. e.printStackTrace();
  89. }
  90. }
  91.  
  92. @Override
  93. public void onDestroy() {
  94. super.onDestroy();
  95. Log.e("CC","onDestroy");
  96. }
  97.  
  98. private class MyTransferListener implements FTPDataTransferListener {
  99. int max;
  100. int count = 0;
  101.  
  102. @Override
  103. public void started() {
  104. Log.e("CC", "started");
  105. max = (int) fileSize;
  106. }
  107.  
  108. @Override
  109. public void transferred(int i) {
  110. count += i;
  111. int percent = count * 100 / max;
  112. sendUploadStatusBroadcast((overAllPercentage + (percent)));
  113. Log.e("CC", "transferring");
  114. if (percent == 100) {
  115. overAllPercentage += (percent);
  116. }
  117. }
  118.  
  119. @Override
  120. public void completed() {
  121. isStopped = true;
  122. Log.e("CC", "compleated");
  123. stopSelf();
  124. }
  125.  
  126. @Override
  127. public void aborted() {
  128. Log.e("CC","aborted");
  129. }
  130.  
  131. @Override
  132. public void failed() {
  133. Log.e("CC","failed");
  134. }
  135. }
  136.  
  137. private void sendUploadStatusBroadcast(int progress) {
  138. Intent uploadProgressIntent = new Intent(getString(R.string.status));
  139. uploadProgressIntent.putExtra("PROGRESS", progress);
  140. LocalBroadcastManager.getInstance(this).sendBroadcast(uploadProgressIntent);
  141. }
  142. }
Add Comment
Please, Sign In to add comment