-1

I need to upload some data like Image and Video to server using Xml web service. I am able to upload using asynch task in Android but I need to upload all data to server in background means there should not be display any progress bar.

So I think to use service for achieving the same, is using a service the right approach? How could I convert activity to service. Are there any other ways to achieve this?

I am writing below my code on button click event for asynch task:

  UploadDataInBackground uploader = new UploadDataInBackground();
  uploader.execute(ReplyForm.this, true, replyform);    // it is asynch class
halfer
  • 19,824
  • 17
  • 99
  • 186
SRam
  • 2,832
  • 4
  • 45
  • 72

3 Answers3

1

You should continue doing this in an AsyncTask. If you do not want a progress dialog to be shown, simpl, remove or comment that part of the the code out. It should be in you onPreExecute (), and onPostExecute ().

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • yeah thats exactly i have done , but i need to refresh the activity list when data uploaded succesfull , and if we leave that activity while executing DoinBackGround() and when it try to refresh list , then force close occurs.. – SRam Aug 28 '12 at 11:40
  • how to schedule the process of uploading in queue? – SRam Sep 01 '12 at 13:13
0

You can do it using ResultReceiver. I have made an application where I upload large video to a server and used something like this:

public class MyResultReceiver extends ResultReceiver {

    public final String TAG = getClass().getSimpleName();
    private Receiver mReceiver;

    public TmResultReceiver(Handler handler) {
        super(handler);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onReceiveResult(int resultCode, Bundle resultData) {
        // TODO Auto-generated method stub
        Log.d("MyResultReceiver", "onReceiveResult");
        if( mReceiver != null ){
            mReceiver.onReceiveResult(resultCode, resultData);
        }
    }

    public void setReceiver(Receiver receiver) {
        //Log.d("MyResultReceiver", "setReceiver");
        mReceiver = receiver;
    }

    public interface Receiver {
        public void onReceiveResult(int resultCode, Bundle resultData);
    }   

}
Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103
0

just implement your own Service, see this post for downloading file using Services, but you must implement your own uploadService` ,, Download a file with Android, and showing the progress in a ProgressDialog

Community
  • 1
  • 1
Mohammad Ersan
  • 12,304
  • 8
  • 54
  • 77