2017-10-09 5 views
2

私はClojureで書いたかなり単純なアプリを持っていて、自動的にその機能の1つを定期的に実行したいと考えています。 AndroidのAlarmManagerを使用してタスクをスケジュールしようとしています。ClojureでAndroidサービスを作成する

Androidのドキュメントを参照enter link description here Clojureの中

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(); 
     } 
    } 
} 

私自身の進歩のために:これは私がこれまで持っているものである

(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'がメソッドとして公開されていて、通常のシンボルではないことに気づいたのです。 –

関連する問題