3

我想在我的Titanium應用程序中使用後臺服務。Android後臺服務

我使用了每個需要在Titanium Android應用程序中插入的代碼。

在TiApp.xml文件:這裏

<services> 
 
    <service url='BGServ.js' type="interval"/> 
 
</services>

這裏註冊服務 「BGServ.js」 文件被放置在我應用程序/ lib目錄文件夾。


在BGServ.js文件:我們的服務文件代碼

var service = Titanium.Android.currentService; 
 
var intent = service.intent; 
 
Ti.API.info('Background Service Started'); 
 
Ti.API.info('Background Service of Platform : ' + Titanium.Platform.osname + ' Started'); 
 

 
service.addEventListener('resume', function(e) { 
 
\t Titanium.API.info('Service code resumes, iteration ' + e.iteration); 
 
}); 
 

 
service.addEventListener('pause', function(e) { 
 
\t Titanium.API.info('Service code pauses, iteration ' + e.iteration); 
 
});


在index.js文件:在這裏,我們創建服務意向

var intent = Titanium.Android.createServiceIntent({ 
 
\t url : '/BGServ.js' 
 
}); 
 
intent.putExtra('interval', 1000); 
 
intent.putExtra('message_to_echo', 'Test Service'); 
 
Titanium.Android.startService(intent);


問題:Android的服務是不是在我的項目工作。在BGServ.js中有一個Ti.API.info(),它也不在控制檯中打印。我是結構在這裏幫助我。

TiSDK版本:3.5.0 GA

謝謝,

Abidhusain

回答

0

實測值後臺服務的Android版解決方案在鈦Appcelerator的

在TiApp。 xml文件:

<services> 
 
    <service type="interval" url="bg_service.js"/> 
 
</services>

注意,這裏的URL屬性無需添加「\」領先的文件名。

並且還將背景服務文件放在您的應用程序的「資產」文件夾中。

在「bg_service。JS「文件:我們的服務文件代碼

var service = Titanium.Android.currentService; 
 
var intent = service.intent; 
 
Ti.API.info('Background Service Started'); 
 
Ti.API.info('Background Service of Platform : ' + Titanium.Platform.osname + ' Started'); 
 

 
service.addEventListener('resume', function(e) { 
 
\t Titanium.API.info('Service code resumes, iteration ' + e.iteration); 
 
}); 
 

 
service.addEventListener('pause', function(e) { 
 
\t Titanium.API.info('Service code pauses, iteration ' + e.iteration); 
 
});

在Index.xml文件:在這裏,我們創造的意圖和啓動後臺服務

var intent = Titanium.Android.createServiceIntent({ 
 
    url : 'bg_service.js' 
 
}); 
 
intent.putExtra('interval', BG_INTERVAL_SECONDS); 
 

 
Titanium.Android.startService(intent);

注意,這裏的URL屬性無需添加「\」領先的文件名。

Issue Resolved by above code and also take care of **"\"** in url attribute 

如果有人發現任何其他解決方案,然後重播這裏。

感謝,

Abidhusain