1

I need to sync data to the server,but I don't want the user to get interrupted in UI,this syncing process should be done in background and even if the user moves to next activity or does perform some action that to finish the current activity but the syncing process should not be cancelled...is there any possibilities? need help thanks in advance...

Asif Sb
  • 785
  • 9
  • 38
  • [Have a look at this](http://stackoverflow.com/questions/15524280/service-vs-intent-service) – nobalG Mar 16 '15 at 11:24

2 Answers2

0

You're looking for IntentService

http://developer.android.com/reference/android/app/IntentService.html

public class YourIntentService extends IntentService {

    public YourIntentService() {
        super("YourIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        // do your syncing here
        // this will run in the background, not on the UI thread
    }
}

Somewhere in your Activity you run:

startService(new Intent(this, YourIntentService.class));

of course you can add extras to the intent, or specify an action.

ci_
  • 8,594
  • 10
  • 39
  • 63
  • All syncing methods I have kept inside a async task...Is it possible to call async task inside onHandleIntent().? – Asif Sb Mar 20 '15 at 07:14
  • Probably not a good idea, and not necessary. `onHandleIntent` is already run on a different thread so no need for `AsyncTask`. If you want to use an `AsyncTask`, you could use a normal `Service` and run it from `onStartCommand()`. Of course you'd have to manually call `stopSelf()` when you're done. – ci_ Mar 20 '15 at 10:46
0

By default an Android application runs in its own process if the process gets killed any code bind with it whether Service or Thread gets killed with the process. But there exists Service, You can bind Service in a different process as well. Check this Tutorial.

Jitender Dev
  • 6,907
  • 2
  • 24
  • 35