4

I've created an application which does synchronization with the server. I've already have a code, which does synchronization once user pressed a button. Now it is time to add Service there. I have the following questions with regards to the services on android:

  1. will the service be started if user never run application before? (i.e. just installed that)
  2. when the service will be started first time? can I start it from onCreate of the main application?
  3. if user presses Synchronize button in the application, should I start that Service or should I have different process for the same? How can I check then that background synchronization is not happening at the same time?
  4. should I use ASyncTask, even if the service is started as

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

LA_
  • 19,823
  • 58
  • 172
  • 308
  • @mgv, I remember about it and will do so soon - just need to review all my questions again (unfortunately, not all of them are answered). – LA_ Apr 02 '11 at 10:30

1 Answers1

2

The preferred approach to syncing data in Android is providing a SyncAdapter to perform the sync. You have a very nice summary of the required steps in this post.

You will also find these articles useful.

Edit:

1-2-3: You can use a bound service to manage the interaction and service methods from your activity. You have full working samples in the link provided. Basically, you bind to the service in your activity's onStart method and unbind onStop.

Remember that a service runs on the UI thread, so every time-consuming task such as fetching data over the network must be done in a separate thread.

4: You don't need an AsyncTask here.

Community
  • 1
  • 1
mgv
  • 8,384
  • 3
  • 43
  • 47
  • Can I use it if I don't want to add account details to "Accounts & Sync" screen and would like to display them just in my application? – LA_ Mar 31 '11 at 19:46
  • Unfortunately you *need* to create an account. However, I can't think of a scenario in which creating an account hurts your users. To the contrary, you will play nice with the android ecosystem and give your users the flexibility to sync the desired content. It's nice to also delegate the responsibility of queuing sync attempts to the system and don't have to handle it yourself. – mgv Mar 31 '11 at 19:47
  • Authorization happens thru OAuth. Have no idea what user will have to input as user name and password ;). – LA_ Mar 31 '11 at 19:53
  • Twitter uses OAuth for example, and you can add a Twitter account :). Basically, you are in charge of implementing the code that will handle the authentication token. – mgv Mar 31 '11 at 19:59
  • Ok, I see that now. So I can implement any authorization I need. However, just a few applications use this (right) approach. But the samples looks a little bit complicated for me now, so will leave that for Phase 2 of my project ;). Could you please help me with service this time? ;) – LA_ Mar 31 '11 at 20:06
  • Thank you! If I implement the approach you've initially suggested - (1) is it possible to open that "Accounts & Sync" screen (or even Add new account of my app) from my application, let's say, by pressing button? (2) will user be able to start synchronization manually directly from my application? If yes, then probably this approach is better to go. – LA_ Mar 31 '11 at 20:22
  • 1) Of course, you just have to check if a valid account exists and fire any activity you want if it doesn't. 2) Sure. Read my last comment on the jcwenger answer I've posted before: http://stackoverflow.com/questions/5253858/why-does-contentresolver-requestsync-not-trigger-a-sync/5255360#5255360 – mgv Mar 31 '11 at 20:28
  • Ok, I've added accounts and syncadapter to my application. ContentProvider is dummy in my case (http://stackoverflow.com/questions/4649808/syncadapter-without-a-contentprovider). How should I define frequency of updates? – LA_ Apr 02 '11 at 20:11
  • You should not ask questions in the comments section. Place a new question for each, so it appears in the search results. Your original question has already been answered. – mgv Apr 02 '11 at 20:57
  • OK, I've raised separate question about the same. However, with regards to the original question - looks like usage of SyncAdapter wasn't the best approach - I am not able to change the freq of synchronization on API 7 (http://stackoverflow.com/questions/5525593/how-to-define-frequency-of-syncadapter-updates-on-android) – LA_ Apr 02 '11 at 21:29