0

I'm porting an application from C# to Android and I need some help to decide which service type i'd need to use on Android to achieve the same result.

Currently theres 2 seperate application. An updater and a client. The updater polls the webserver for updates every 5 seconds. Whenever an update is available it downloads it then warns the client that there's new content available.

The client then displays the content onscreen.

Note: This software will be run on a android player and its sole purpose is to run these 2 applications. Nothing more. There will be no user interaction with it.

So my question is what service would I need to use for this?

Kind Regards, Niek

Deejdd
  • 119
  • 1
  • 1
  • 8
  • http://developer.android.com/google/gcm/index.html – Selvin May 17 '13 at 09:37
  • I'm sorry, but that's not quite what I'm looking for. The updater and client must be both on the android device. – Deejdd May 17 '13 at 09:41
  • 1
    what for? changes are made on server ... after server make some changes then send GCM message to android client ... client will download new data ... making polls ever 5 sec will drain battery – Selvin May 17 '13 at 10:06
  • Battery life isn't an issue as we're using android players. Which you hook directly in a tv and are 24/7 powered on with a power supply. But I do thank you for the link, it was an interresting read. – Deejdd May 17 '13 at 10:41

1 Answers1

0

You can check the following links http://developer.android.com/reference/android/app/Service.html http://developer.android.com/guide/components/services.html http://www.vogella.com/articles/AndroidServices/article.html

You can add a thread which will download code after every 5 seconds. Here is the code

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service {

   String tag="TestService";
   @Override
   public void onCreate() {
       super.onCreate();
       Toast.makeText(this, "Service created...", Toast.LENGTH_LONG).show();      
       Log.i(tag, "Service created...");
   }

   @Override
   public void onStart(Intent intent, int startId) {      
       super.onStart(intent, startId);  
       Log.i(tag, "Service started...");
       fetchData();
   }

   public void fetchData()
   {
   new Thread(new Runnable()
                    {

                        @Override
                        public void run()
                            {
                            while(true)
                            {
                                final String strRes = downloadXML("http://uri.com");
                                runOnUiThread(new Runnable()
                                    {

                                        @Override
                                        public void run()
                                            {
                                                //do whatever once download complete
                                            }
                                    });
                                    try{
                                    Thread.sleep(5000);
                                    }catch(Exception ee){}
                            }
                            }
                    }).start();
   }
   @Override
   public void onDestroy() {
       super.onDestroy();
       Toast.makeText(this, "Service destroyed...", Toast.LENGTH_LONG).show();
   }

   @Override
   public IBinder onBind(Intent intent) {
       return null;
   }
}
Sandeep
  • 1,814
  • 21
  • 25
  • 1
    don't forget to make service entry in manifest – Sandeep May 17 '13 at 10:00
  • What will happen if the activity crashes, does this terminate the service aswell? Because I'm trying to prevent that from happening. The service should keep the application alive and vice versa. Thanks for the piece of code and the links, they were very useful. – Deejdd May 17 '13 at 10:06
  • bad idea ... you should use AlarmManager instead http://developer.android.com/reference/android/app/AlarmManager.html using `Thread.Sleep()` is bad code example – Selvin May 17 '13 at 10:06
  • 1
    For every 5 second, it would be good to use service. You start it on boot. – Sandeep May 17 '13 at 10:07
  • Does this type of service allow me to restart the application when it crashes or does it get terminated aswell? – Deejdd May 17 '13 at 10:37
  • 1
    You can check the developer site link for more details. – Sandeep May 17 '13 at 10:40