예제 #1
0
 /** Stop {@link MyService} asynchronously */
 public static synchronized void stopService() {
   if (!MyContextHolder.get().isReady()) {
     return;
   }
   // Don't do "context.stopService", because we may lose some information and (or) get Force Close
   // This is "mild" stopping
   CommandData element = new CommandData(CommandEnum.STOP_SERVICE, "");
   MyContextHolder.get()
       .context()
       .sendBroadcast(element.toIntent(MyAction.EXECUTE_COMMAND.getIntent()));
 }
예제 #2
0
 /**
  * Returns previous service state and queries service for its current state asynchronously.
  * Doesn't start the service, so absence of the reply will mean that service is stopped @See <a
  * href="http://groups.google.com/group/android-developers/browse_thread/thread/8c4bd731681b8331/bf3ae8ef79cad75d">here</a>
  */
 public static MyServiceState getServiceState() {
   synchronized (mServiceState) {
     long time = System.nanoTime();
     if (waitingForServiceState
         && (time - stateQueuedTime)
             > java.util.concurrent.TimeUnit.SECONDS.toMillis(STATE_QUERY_TIMEOUT_SECONDS)) {
       // Timeout expired
       waitingForServiceState = false;
       mServiceState = MyServiceState.STOPPED;
     } else if (!waitingForServiceState && mServiceState == MyServiceState.UNKNOWN) {
       // State is unknown, we need to query the Service again
       waitingForServiceState = true;
       stateQueuedTime = time;
       mServiceState = MyServiceState.UNKNOWN;
       CommandData element = new CommandData(CommandEnum.BROADCAST_SERVICE_STATE, "");
       MyContextHolder.get()
           .context()
           .sendBroadcast(element.toIntent(MyAction.EXECUTE_COMMAND.getIntent()));
     }
   }
   return mServiceState;
 }