0

I am working on an application which has 5- 6 activities where user is involved. Some data will be generated during this and is saved into Database.

I need to upload/sync these data between server ( I use KSOAP2 ). But this should happen behind. Even if the user is jumping between activities, it should not get affected. But when I quit the app I don't want this process to continue.

Is Android Service the only way to do this? What would be the best option?

  • IntentService exactly does what you want. Why do you prefer not to use it? Refer to this link for a good comparison with AsyncTask http://stackoverflow.com/a/4177349/530039 – akaya Mar 21 '13 at 14:57
  • OK.. thanks. But in my case, 1. I don't want to notify the user that something is happening. While user is filling some entries in various activities,my upload process ( soap calls ) should happen. i.e it should be a very background thing and I don't want user to interrupt his work. This is possible ? –  Mar 22 '13 at 07:44
  • You don't have to notify the user at all. Services are meant to be a very background thing like you want. Try it. – akaya Mar 22 '13 at 08:19

3 Answers3

1

You can use Async Task

http://developer.android.com/reference/android/os/AsyncTask.html. Have a look at the documentation under the heading The 4 steps.

AsyncTask is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent pacakge such as Executor, ThreadPoolExecutor and FutureTask.

In your activity after setContentView()

    pd= new ProgressDialog(this);
    pd.setTitle("Making Soap Request");
     new SoapRequestTask().execute();

 private class SoapRequestTask extends AsyncTask<VOid, Void, Void> {

  protected void onPreExecute()
  {
      pd.show();
  }
   protected SoapObject doInBackground(Void... params) {
    // TODO Auto-generated method stub
           //Soap request. do not update ui here
    return null;
    }

 protected void onPostExecute(Void param)
 {   
      //update ui here
  pd.dismiss();

 }

An alternative to asynctask is Robospice.https://github.com/octo-online/robospice.

You can get started with robopice here. https://github.com/octo-online/robospice/wiki/Starter-Guide.

A sample of robospice at https://play.google.com/store/apps/details?id=com.octo.android.robospice.motivations&feature=search_result.

Some of the features of robospice.

1.executes asynchronously (in a background AndroidService) network requests (ex: REST requests using Spring Android).

2.is strongly typed ! You make your requests using POJOs and you get POJOs as request results.

3.enforce no constraints neither on POJOs used for requests nor on Activity classes you use in your projects.

4.caches results (in Json with both Jackson and Gson, or Xml, or flat text files, or binary files, even using ORM Lite).

5.notifies your activities (or any other context) of the result of the network request if and only if they are still alive

6.no memory leak at all, like Android Loaders, unlike Android AsyncTasks notifies your activities on their UI Thread.

7.uses a simple but robust exception handling model.

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • OK.. thanks. But in my case, 1. I don't want to notify the user that something is happening. While user is filling some entries in various activities,my upload process ( soap calls ) should happen. i.e it should be a very background thing and I don't want user to interrupt his work. This is possible through AsyncTask? –  Mar 22 '13 at 07:43
  • In that case you have to use service if its a long running operation or check robospice. – Raghunandan Mar 22 '13 at 07:49
0

I think Async task is what you are looking for ...http://developer.android.com/reference/android/os/AsyncTask.html

DntFrgtDSemiCln
  • 1,259
  • 2
  • 16
  • 35
0

No, you can use an AsyncTask. documentation: http://developer.android.com/reference/android/os/AsyncTask.html

Emil Adz
  • 40,709
  • 36
  • 140
  • 187