public void onDestroy() {

    if (server != null) server.stop();

    Log.d(TAG, "HttpService Stopped");
  }
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {

    if (intent == null) {
      stopSelf();
      return super.onStartCommand(intent, flags, startId);
    }
    String type = intent.getStringExtra(TYPE_KEY);
    if (type == null) {
      type = intent.getType();
    }
    if (type == null) {
      stopSelf();
      return super.onStartCommand(intent, flags, startId);
    }

    if (type.startsWith("keyboard/key")) {

      Message msg = mServiceHandler.obtainMessage();
      int key = intent.getIntExtra(KeyboardActivity.KEYBOARD_KEY, -1);

      if (key != -1) {
        Log.d(TAG, "KeyEvent loaded : " + KeyEvent.keyCodeToString(key));
        JSONObject jsonPostObject = new JSONObject();
        try {
          jsonPostObject.put("value", key);
          jsonPostObject.put("mime", "keyboard/key");
        } catch (JSONException e) {
          e.printStackTrace();
        }
        msg.obj = jsonPostObject;
        mServiceHandler.sendMessage(msg);
      }

    } else if (intent.hasExtra(Intent.EXTRA_TEXT)) {

      Message msg = mServiceHandler.obtainMessage();
      String txt = intent.getStringExtra(Intent.EXTRA_TEXT);

      if (txt != null) {
        Log.d(TAG, "Text loaded : " + txt);
        JSONObject jsonPostObject = new JSONObject();
        try {
          jsonPostObject.put("value", txt);
          jsonPostObject.put("mime", "text/plain");
        } catch (JSONException e) {
          e.printStackTrace();
        }
        msg.obj = jsonPostObject;
        mServiceHandler.sendMessage(msg);
        // BroadcastTask task = new BroadcastTask(getApplicationContext());
        // task.execute(BroadcastTask.SEND_TEXT, txt, type);
      }

    } else if (intent.hasExtra(Intent.EXTRA_STREAM)) {

      Uri streamUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
      File file = AndroidUtils.getFile(this, streamUri, false);
      Log.d(TAG, "Stream loaded : " + file.getPath());

      try {
        if (server != null) {
          server.stop();
        }
        server = new NanoHTTPDSender(0, null, this);

        Message msg = mServiceHandler.obtainMessage();
        Uri uri = Uri.fromFile(file);
        Log.d(TAG, "File Uri : " + uri);
        String filePath = server.addFile(mHttpService, uri);
        Log.d(TAG, "Server File path : " + filePath);
        URL ext = new URL(new URL(server.getLocalhost()), filePath);
        Log.d(TAG, "External URL : " + ext.toExternalForm());

        String mimetype = null;
        String extension = "*";
        if (file.getName().contains(".")) {
          extension = AndroidUtils.extension(file.getName());
          mimetype = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        }
        if (mimetype == null) {
          if (type.startsWith("*/")) {
            mimetype = "application/" + extension;
          } else {
            mimetype = type.replace("/*", "/" + extension);
          }
        }
        JSONObject jsonPostObject = new JSONObject();
        try {
          jsonPostObject.put("value", ext.toExternalForm());
          jsonPostObject.put("mime", mimetype != null ? mimetype : type);
          jsonPostObject.put("extension", extension);
        } catch (JSONException e) {
          e.printStackTrace();
        }
        msg.obj = jsonPostObject;
        mServiceHandler.sendMessage(msg);
      } catch (MalformedURLException e) {
        Log.d(TAG, Log.getStackTraceString(e));
      } catch (IOException e) {
        Log.d(TAG, Log.getStackTraceString(e));
      }
    }

    return super.onStartCommand(intent, flags, startId);
  }