@Override
 public void onCreate() {
   /*
    * Activity may ask us to shutdown or restart, we don't want this to
    * happen on accident (and we don't want to bind the service, that's why
    * intents are used), that's why we generate a random long String to use
    * as an intent filter
    */
   Common.serviceBR = BigIntegerGenerator.nextSessionId();
   Log.i(tag, "password is: " + Common.serviceBR);
   air = new ActivityIntentReceiver();
   registerReceiver(air, new IntentFilter(Common.serviceBR));
   Common.serviceClassName = this.getClass().getName();
 }
public class SystemControllerService extends Service {
  Handler launcherHandler = new Handler();

  private static final String tag = SystemControllerService.class.getName();

  private String componentsPassword = BigIntegerGenerator.nextSessionId();

  private boolean bluetoothPermitted = false;

  /*
   * The SysCtrlServLauncherActivity may ask us to shutdown or reset
   */
  ActivityIntentReceiver air;

  // contains ssm functionality
  CWrapper wrapper = new CWrapper();

  // empty method stub
  @Override
  public IBinder onBind(Intent arg0) {
    return null;
  }

  @Override
  public void onCreate() {
    /*
     * Activity may ask us to shutdown or restart, we don't want this to
     * happen on accident (and we don't want to bind the service, that's why
     * intents are used), that's why we generate a random long String to use
     * as an intent filter
     */
    Common.serviceBR = BigIntegerGenerator.nextSessionId();
    Log.i(tag, "password is: " + Common.serviceBR);
    air = new ActivityIntentReceiver();
    registerReceiver(air, new IntentFilter(Common.serviceBR));
    Common.serviceClassName = this.getClass().getName();
  }

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    if (startId != 1) {
      Log.e(tag, "Trying to start " + this.getClass().getSimpleName() + " more than once!");
      return START_NOT_STICKY;
    }
    bluetoothPermitted = intent.getBooleanExtra("bluetooth", false);
    Log.i(tag, "starting SSM  thread");
    if (Config.sendMails) {
      LogsRecorder.startRecording();
    }
    wrapper.start(new ComponentLauncher(this), Config.useAuthentication);
    startForeground(1, NotificationHandler.createMainNotification(this));
    return START_NOT_STICKY; // is set as a foreground service anyway
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    unregisterReceiver(air);
  }

  /* launcher functions */

  void launchService(Class<?> serviceClass) {
    Log.v(tag, Logging.getCurrentMethodName(serviceClass));
    startService(new Intent(this, serviceClass).putExtra("password", componentsPassword));
  }

  void launchConnectivityAgent() {
    Log.v(tag, Logging.getCurrentMethodName());
    startService(
        new Intent(this, ConnectivityAgentService.class)
            .putExtra("password", componentsPassword)
            .putExtra("bluetooth", bluetoothPermitted));
  }

  void launchChannelSupervisor() {
    Log.v(tag, Logging.getCurrentMethodName());
    launchService(ChannelSupervisorService.class);
  }

  void launchProfileManager() {
    Log.v(tag, Logging.getCurrentMethodName());
    launchService(ProfileLayerService.class);
  }

  void launchApplicationManager() {
    Log.v(tag, Logging.getCurrentMethodName());
    launchService(ApplicationManagerService.class);
  }

  public void launchAuthentication() {
    Log.v(tag, Logging.getCurrentMethodName());
    startActivity(
        new Intent(this, AuthenticationActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
  }

  private void doShutdown(final boolean reset) {
    Log.v(tag, Logging.getCurrentMethodName(reset));
    sendBroadcast(new Intent(IntentActions.KILLER_APP));
    sendBroadcast(new Intent(componentsPassword));
    if (Config.sendMails) {
      Toast.makeText(this, "Sending mail with logs now, will shutdown shortly", Toast.LENGTH_LONG)
          .show();
    }
    // to avoid network on main thread exception if mails are sent
    new Thread(
            new Runnable() {
              public void run() {
                if (Config.sendMails) {
                  LogsRecorder.mailLog();
                  Log.i(tag, "mailing done!");
                } else {
                  // we are giving time for anti-ivilink to kick in
                  try {
                    Thread.sleep(1000);
                  } catch (InterruptedException e) {
                    e.printStackTrace();
                  }
                }
                if (reset) {
                  AlarmHandler.setWakeApp(
                      SystemControllerService.this, AlarmHandler.RESTART_TIMEOUT);
                }
                android.os.Process.killProcess(android.os.Process.myPid());
              }
            })
        .start();
  }

  private void allOk() {
    Log.v(tag, "allOk() - switching to the foreground state");
    sendBroadcast(new Intent(Common.ifProgress).putExtra(Common.message, Common.doneLaunch));
    launcherHandler.post(
        new Runnable() {
          public void run() {
            Toast.makeText(SystemControllerService.this, Common.doneLaunch, Toast.LENGTH_LONG)
                .show();
          }
        });
  }

  class ActivityIntentReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
      Log.v(tag, "intent came from the Launcher Activity");
      String misc = intent.getStringExtra(Common.misc);
      if (misc.equals(Common.reset)) {
        doShutdown(true);
      } else if (misc.equals(Common.shutdown)) {
        doShutdown(false);
      } else if (misc.equals(Common.doneLaunch)) {
        allOk();
      }
    }
  }
}