How to upload file to server using background service

by / Wednesday, 14 August 2013 / Published in Android

A Service is an application component that can perform long-running operations in the background and does not provide a user interface. Another application component can start a service and it will continue to run in the background even if the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service might handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.
Here is an simple example to upload a file to the server using background service.

Manifest.xml

At first we have to declare a service in android manifest.xml

<service android:name="SimpleService" >
<intent-filter>
<action android:name="in.mettletech.uoloadfilebackgroundservice.SimpleService">
</action>
</intent-filter>
</service>

 

UploadloadToFtp.java

package in.mettletech.uoloadfilebackgroundservice;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;

public class UploadToFtp {

public boolean ftpUpload1(String srcFilePath,String desFileName)
{

boolean status = false;

String host="host";
String username="username";
String password="password";
int port="123";

String SFTPWORKINGDIR = "destination";
String FILETOTRANSFER=srcFilePath;

FTPClient mFTPClient = new FTPClient();
try {

// connecting to the host
mFTPClient.connect(host, port);
mFTPClient.login(username, password);

mFTPClient.changeWorkingDirectory(SFTPWORKINGDIR);
mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
BufferedInputStream buffIn = null;
File file=new File(FILETOTRANSFER);
buffIn = new BufferedInputStream(new FileInputStream(file),8192);
mFTPClient.enterLocalPassiveMode();

status = mFTPClient.storeFile(desFileName, buffIn);

buffIn.close();
mFTPClient.logout();
mFTPClient.disconnect();

} catch (Exception e) {
e.printStackTrace();

}

return status;
}

}

SimpleService.java

package in.mettletech.uoloadfilebackgroundservice;

import java.io.File;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.Environment;
import android.os.IBinder;
import android.widget.Toast;

public class SimpleService extends Service {

private MyThread myythread;
public boolean isRunning = false;
long interval=30000;

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

@Override
public void onCreate() {
super.onCreate();

myythread  = new MyThread(interval);
}

@Override
public synchronized void onDestroy() {
super.onDestroy();
if(!isRunning){
myythread.interrupt();
myythread.stop();
}
}

@Override
public synchronized void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
if(!isRunning){
myythread.start();
isRunning = true;
}
}

class MyThread extends Thread{
long interval;
public MyThread(long interval){
this.interval=interval;
}
@Override
public void run(){
while(isRunning){
System.out.println("Service running");
try {
fileUpload();
Thread.sleep(interval);
} catch (InterruptedException e) {
isRunning = false;
e.printStackTrace();
}
}
}
private void fileUpload() {

String url=Environment.getExternalStorageDirectory()+"/new_Account.txt";
if(checkInternetConnection()){
boolean upload_status=new UploadToFtp().ftpUpload1(url, "new_Account.txt");
}
}

}

private boolean checkInternetConnection() {

ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isAvailable() && cm.getActiveNetworkInfo().isConnected()) {
return true;
}
else
{

return false;
}
}

}

To start and stop a  service use these following lines:

Intent i=new Intent(MainActivity.this,SimpleService.class);

startService(i);

to stop:

Intent i=new Intent(MainActivity.this,SimpleService.class);

stopService(i);

Tagged under: , ,
TOP