2016-08-03 61 views
1

我正在使用appcelerator工作室構建服務。這是我在建設Index.js代碼頁:如何使用appcelerator構建服務Android

var intent = Titanium.Android.createServiceIntent({ url: 'myservice.js' }); 
// Service should run its code every 2 seconds. 
intent.putExtra('interval', 60000); 
// A message that the service should 'echo' 
//intent.putExtra('message_to_echo', 'Bo uscirà questo messaggio?'); 

var service = Titanium.Android.createService(intent); 
service.addEventListener('resume', function(e) { 
    Titanium.API.info('Service code resumes, iteration ' + e.iteration); 
}); 
service.addEventListener('pause', function(e) { 
    Titanium.API.info('Service code pauses, iteration ' + e.iteration); 
    if (e.iteration === 1) { 
     var _model = Alloy.createModel("ServiceDAO", { 
      ID : 1, 
      ServiceRunning: 0, 
      ApplicationRunning: 0 
     }); 
    _model.save();  
    } 
}); 
service.start(); 

現在,當我嘗試啓動應用程序時,myservice.js是執行每60秒,但我有兩個問題:

1)當服務運行時,我有一個接口的性能問題 2)如果我關閉應用程序,服務沒有運行。

那麼我怎麼能實施一個服務與appcelerator運行在後臺也應用程序不運行?

+0

你可能需要建立爲 –

+0

模塊你能解釋一下我怎樣才能建立爲 – bircastri

+0

HTTPS模塊:// wiki.appcelerator.org/display/guides2/Android+Module+Development+Guide –

回答

0

我試過一次:http://docs.appcelerator.com/platform/latest/#!/guide/Android_Services-section-43287937_AndroidServices-ServiceCode,似乎工作正常。 要在應用程序關閉時進行日誌記錄,則應使用ADB logcat。 另外,不要忘了把它列入你的tiapp.xml這樣的:

<services> 
    <service url="your_service_name.js" type="interval"/> 
</services> 
+0

我剛剛在我的Tiapp.xml中插入了這段代碼 而且我的服務能夠正常工作,但是隻有當應用程序在昨天運行 – bircastri

+0

時,我發現它需要有一點讓ADB在我殺了應用程序後記錄我的代碼。我每10秒鐘都有一個日誌,但是當我關閉我的應用程序時,大約需要2分鐘,而不是每10秒鐘重新開始一次。 順便說一句,我正在使用'''adb logcat | grep「TiAPI」'''僅記錄Titanium API –