2017-10-09 52 views
2

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

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也不是。

回答

0

此代码有效,但只有在将该项目添加到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")) 
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也可能提供一些有用的见解。希望这有助于...

+0

谢谢;现在我越来越近了,特别是它帮助我们认识到'superIntentService'是作为一种方法公开的,而不是一个普通的符号。 –