【问题标题】:Creating an Android Service in Clojure在 Clojure 中创建 Android 服务
【发布时间】:2017-10-09 02:04:08
【问题描述】:

我有一个相当简单的应用程序,它是用 Clojure 编写的,并希望定期自动执行其中一个功能。我正在尝试使用 Android 的 AlarmManager 来安排任务。这是我目前所拥有的:

Android 的文档供参考enter link description here

public class HelloIntentService extends IntentService {

  /**
   * A constructor is required, and must call the super IntentService(String)
   * constructor with a name for the worker thread.
   */
  public HelloIntentService() {
      super("HelloIntentService");
  }

  /**
   * The IntentService calls this method from the default worker thread with
   * the intent that started the service. When this method returns, IntentService
   * stops the service, as appropriate.
   */
  @Override
  protected void onHandleIntent(Intent intent) {
      // Normally we would do some work here, like download a file.
      // For our sample, we just sleep for 5 seconds.
      try {
          Thread.sleep(5000);
      } catch (InterruptedException e) {
          // Restore interrupt status.
          Thread.currentThread().interrupt();
      }
  }
}

我自己在 Clojure 中的进步:

(gen-class
 :name adamdavislee.mpd.Service
 :extends android.app.IntentService
 :exposes-methods {IntentService superIntentService}
 :init init
 :prefix service)

(defn service-init []
  (superIntentService "service")
  [[] "service"])
(defn service-onHandleIntent [this i]
  (toast "hi"))

我想我误解了一些微妙的东西;在评估第一个 sexp 后,符号 adamdavislee.mpd.Service 未绑定,符号 superIntentService 也未绑定。

【问题讨论】:

    标签: android clojure gen-class


    【解决方案1】:

    将根据阅读您的代码提出一些建议(即不确定这些是否可行)

    您似乎对 Java 互操作有疑问。您可以查看更多信息here。看起来:prefix 也应该是一个字符串。例如

    (gen-class
     :name adamdavislee.mpd.Service
     :extends android.app.IntentService
     :exposes-methods {IntentService superIntentService}
     :init init
     :prefix "service-")  ;;---> make this a string and append '-'
    
    (defn service-init []
      (.superIntentService "service");;-> update with prepend dot-notation for Java interop
      [[] "service"])                ;;-> not sure what 'service' is doing here 
                                     ;;   perhaps consider an atom
    (defn service-onHandleIntent [this i]
      (.toast "hi"))  ;;=> Not sure where this method is coming from, 
                      ;;   but guessing java interop need here as well
    

    This example 也可能提供一些有用的见解。希望这会有所帮助...

    【讨论】:

    • 谢谢;我现在越来越接近了,特别是它有助于意识到 superIntentService 是作为方法而不是常规符号公开的。
    【解决方案2】:

    此代码有效,但前提是在添加对gen-class 的调用后重新编译项目。 gen-class 只能在项目编译时生成类。

    (gen-class
     :name adamdavislee.mpd.Service
     :extends android.app.IntentService
     :init init
     :state state
     :constructors [[] []]
     :prefix service-)
    (defn service-init
      []
      [["NameForThread"]
       "NameForThread"])
    (defn service-onHandleIntent
      [this i]
      (toast "service started"))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多