Keep the service running until the JVM shuts down.

This commit is contained in:
akwizgran
2012-11-13 11:07:20 +00:00
parent 8fffc93bbc
commit e1fb603d6b

View File

@@ -16,7 +16,7 @@ import android.os.IBinder;
import com.google.inject.Inject; import com.google.inject.Inject;
public class HelloWorldService extends RoboService implements Runnable { public class HelloWorldService extends RoboService {
private static final Logger LOG = private static final Logger LOG =
Logger.getLogger(HelloWorldService.class.getName()); Logger.getLogger(HelloWorldService.class.getName());
@@ -29,18 +29,23 @@ public class HelloWorldService extends RoboService implements Runnable {
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
if(LOG.isLoggable(INFO)) LOG.info("Created"); if(LOG.isLoggable(INFO)) LOG.info("Created");
Thread t = new Thread(this); new Thread() {
t.setDaemon(false); @Override
t.start(); public void run() {
startServices();
}
}.start();
} }
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
if(LOG.isLoggable(INFO)) LOG.info("Started");
return 0; return 0;
} }
@Override @Override
public IBinder onBind(Intent intent) { public IBinder onBind(Intent intent) {
if(LOG.isLoggable(INFO)) LOG.info("Bound");
return null; return null;
} }
@@ -48,11 +53,16 @@ public class HelloWorldService extends RoboService implements Runnable {
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
if(LOG.isLoggable(INFO)) LOG.info("Destroyed"); if(LOG.isLoggable(INFO)) LOG.info("Destroyed");
new Thread() {
@Override
public void run() {
stopServices();
}
}.start();
} }
public void run() { private void startServices() {
try { try {
// Start...
if(LOG.isLoggable(INFO)) LOG.info("Starting"); if(LOG.isLoggable(INFO)) LOG.info("Starting");
db.open(false); db.open(false);
if(LOG.isLoggable(INFO)) LOG.info("Database opened"); if(LOG.isLoggable(INFO)) LOG.info("Database opened");
@@ -61,11 +71,15 @@ public class HelloWorldService extends RoboService implements Runnable {
int pluginsStarted = pluginManager.start(this); int pluginsStarted = pluginManager.start(this);
if(LOG.isLoggable(INFO)) if(LOG.isLoggable(INFO))
LOG.info(pluginsStarted + " plugins started"); LOG.info(pluginsStarted + " plugins started");
// ...sleep... } catch(DbException e) {
try { if(LOG.isLoggable(WARNING)) LOG.warning(e.toString());
Thread.sleep(30 * 1000); } catch(IOException e) {
} catch(InterruptedException ignored) {} if(LOG.isLoggable(WARNING)) LOG.warning(e.toString());
// ...and stop }
}
private void stopServices() {
try {
if(LOG.isLoggable(INFO)) LOG.info("Shutting down"); if(LOG.isLoggable(INFO)) LOG.info("Shutting down");
int pluginsStopped = pluginManager.stop(); int pluginsStopped = pluginManager.stop();
if(LOG.isLoggable(INFO)) if(LOG.isLoggable(INFO))
@@ -79,6 +93,5 @@ public class HelloWorldService extends RoboService implements Runnable {
} catch(IOException e) { } catch(IOException e) {
if(LOG.isLoggable(WARNING)) LOG.warning(e.toString()); if(LOG.isLoggable(WARNING)) LOG.warning(e.toString());
} }
stopSelf();
} }
} }