-
-
Save mauropm/3189579 to your computer and use it in GitHub Desktop.
Android background service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*global Ti, alert */ | |
var SECS = 5; | |
var URL = 'testservice.js'; | |
var win = Ti.UI.createWindow({ | |
fullscreen: false, | |
navBarHidden: true, | |
exitOnClose: true | |
}); | |
var title = Ti.UI.createLabel({ | |
top: 0, left: 5, right: 5, height: 40, | |
text: 'A service that runs its code every ' + SECS + ' secs. See testservice.js. Console:' | |
}); | |
win.add(title); | |
var console = Ti.UI.createLabel({ | |
top: 45, left: 5, right: 5, height: 200, | |
backgroundColor: 'white', color: 'black', | |
font: {fontSize: 12}, verticalAlign: 'top' | |
}); | |
win.add(console); | |
function addMsg(msg) { | |
Ti.API.info('SERVICE TEST: ' + msg); | |
var text = console.text; | |
if (text && text.length > 0) { | |
text = msg + '\n' + text; | |
} else { | |
text = msg; | |
} | |
console.text = text; | |
} | |
Ti.App.addEventListener('test_service_fire', function(data) { | |
addMsg('Service says: "' + data.message + '"'); | |
}); | |
var checkButton = Ti.UI.createButton({ | |
title: 'Check if Test Service is running', | |
left: 5, right: 5, top: 250, height: 35 | |
}); | |
checkButton.addEventListener('click', function(){ | |
if (Ti.Android.isServiceRunning(Ti.Android.createServiceIntent({url: URL}))) { | |
addMsg('Service IS running'); | |
} else { | |
addMsg('Service is NOT running'); | |
} | |
}); | |
win.add(checkButton); | |
var startedButton = Ti.UI.createButton({ | |
title: 'Start Service via startService()', | |
left: 5, right: 5, top: 290, height: 35 | |
}); | |
startedButton.addEventListener('click', function() { | |
addMsg('Starting via startService'); | |
var intent = Ti.Android.createServiceIntent({ | |
url: URL | |
}); | |
intent.putExtra('interval', SECS * 1000); | |
intent.putExtra('message', 'Hi from started service'); | |
Ti.Android.startService(intent); | |
}); | |
win.add(startedButton); | |
var bindButton = Ti.UI.createButton({ | |
top: 330, left: 5, right: 5, height: 35, | |
title: 'Start Service via createService()/start()' | |
}); | |
bindButton.addEventListener('click', function() { | |
addMsg('Starting via createService() / start()'); | |
var intent = Ti.Android.createServiceIntent({ | |
url: URL | |
}); | |
intent.putExtra('interval', SECS * 1000); | |
intent.putExtra('message', 'Hi from bound service'); | |
var service = Ti.Android.createService(intent); | |
service.addEventListener('start', function(e) { | |
addMsg('Starting... Instance #' + e.source.serviceInstanceId + ' (bound)'); | |
}); | |
service.addEventListener('pause',function(e) { | |
addMsg('Bound instance #' + e.source.serviceInstanceId + ' paused (iteration #' + e.iteration + ')'); | |
if (e.iteration == 3) { | |
addMsg('Bound instance #' + e.source.serviceInstanceId + ' has had 3 iterations... going to stop it now.'); | |
e.source.stop(); | |
} | |
}); | |
service.addEventListener('resume',function(e) { | |
addMsg('Bound instance #' + e.source.serviceInstanceId + ' resumed (iteration #' + e.iteration + ')'); | |
}); | |
service.start(); | |
}); | |
win.add(bindButton); | |
var stopButton = Ti.UI.createButton({ | |
top: 370, left: 5, right: 5, height: 35, | |
title: 'Stop Service via stopService()' | |
}); | |
stopButton.addEventListener('click', function() { | |
var intent = Ti.Android.createServiceIntent({url: URL}); | |
Ti.Android.stopService(intent); | |
addMsg('stopService() called. NOTE: service only stops if no "bound" proxies are still alive. "Bound" proxies are those created with createService()'); | |
}); | |
win.add(stopButton); | |
win.open(); | |
win.addEventListener('open', function() { | |
var activity = win.activity; //Ti.Android.currentActivity; | |
activity.addEventListener('pause', function(){ | |
Ti.API.info(' **** Paused App'); | |
//process(); | |
}); | |
activity.addEventListener('resume', function(){ | |
Ti.API.info(' **** resume App'); | |
}); | |
}); | |
win.addEventListener('android:back', function(){ | |
var intent = Ti.Android.createIntent({ | |
action: Ti.Android.ACTION_MAIN | |
}); | |
intent.addCategory(Ti.Android.CATEGORY_HOME); | |
Ti.Android.currentActivity.startActivity(intent); | |
}); | |
function process(){ | |
addMsg('Starting via createService() / start()'); | |
var intent = Ti.Android.createServiceIntent({ | |
url: URL | |
}); | |
intent.putExtra('interval', SECS * 1000); | |
intent.putExtra('message', 'Hi from bound service'); | |
var service = Ti.Android.createService(intent); | |
service.addEventListener('start', function(e) { | |
addMsg('Starting... Instance #' + e.source.serviceInstanceId + ' (bound)'); | |
}); | |
service.addEventListener('pause',function(e) { | |
addMsg('Bound instance #' + e.source.serviceInstanceId + ' paused (iteration #' + e.iteration + ')'); | |
if (e.iteration == 3) { | |
addMsg('Bound instance #' + e.source.serviceInstanceId + ' has had 3 iterations... going to stop it now.'); | |
e.source.stop(); | |
} | |
}); | |
service.addEventListener('resume',function(e) { | |
addMsg('Bound instance #' + e.source.serviceInstanceId + ' resumed (iteration #' + e.iteration + ')'); | |
}); | |
service.start(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*global Ti */ | |
var service = Ti.Android.currentService; | |
if(!service){ | |
Ti.API.info("I'm zombie."); | |
}else{ | |
var service_intent = service.getIntent(); | |
var my_data = service_intent.getStringExtra('message') + ' (instance ' + service.serviceInstanceId + ')'; | |
Ti.App.fireEvent('test_service_fire', { message: my_data}); | |
Ti.API.info(my_data); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<ti:app xmlns:ti="http://ti.appcelerator.org"> | |
<property name="acs-oauth-secret-production" type="string">6dYZ1jIVY6VgITxwIrVjsvzjv7svkBrk</property> | |
<property name="acs-oauth-key-production" type="string">YFPLjtx5rJHIrIFTR84DBeZRXIAce5P4</property> | |
<property name="acs-api-key-production" type="string">lqAS1lfSaBtkS9Hb7c9Mz0dEy3Bemb9z</property> | |
<property name="acs-oauth-secret-development" type="string">ZQaNRi6MnrvIcOlZpV6A2qfkHOINwTO1</property> | |
<property name="acs-oauth-key-development" type="string">pGCbX3HwTy0s5EVHoWGffLnTdzrogrfu</property> | |
<property name="acs-api-key-development" type="string">hvDltD8251wNjrjvEEQpw4fNElKVo0v0</property> | |
<deployment-targets> | |
<target device="mobileweb">true</target> | |
<target device="iphone">true</target> | |
<target device="ipad">true</target> | |
<target device="android">true</target> | |
<target device="blackberry">false</target> | |
</deployment-targets> | |
<sdk-version>2.0.2.GA</sdk-version> | |
<id>com.appc.test</id> | |
<name>1Testy</name> | |
<version>1.0</version> | |
<publisher>egomez</publisher> | |
<url>http://</url> | |
<description>not specified</description> | |
<copyright>2012 by egomez</copyright> | |
<icon>appicon.png</icon> | |
<persistent-wifi>false</persistent-wifi> | |
<prerendered-icon>false</prerendered-icon> | |
<statusbar-style>default</statusbar-style> | |
<statusbar-hidden>false</statusbar-hidden> | |
<fullscreen>false</fullscreen> | |
<navbar-hidden>false</navbar-hidden> | |
<analytics>true</analytics> | |
<guid>379543c4-7170-4756-ab7a-452e829c7d18</guid> | |
<property name="ti.ui.defaultunit">system</property> | |
<property name="ti.facebook.appid">417551948276805</property> | |
<iphone> | |
<orientations device="iphone"> | |
<orientation>Ti.UI.PORTRAIT</orientation> | |
</orientations> | |
<orientations device="ipad"> | |
<orientation>Ti.UI.PORTRAIT</orientation> | |
<orientation>Ti.UI.UPSIDE_PORTRAIT</orientation> | |
<orientation>Ti.UI.LANDSCAPE_LEFT</orientation> | |
<orientation>Ti.UI.LANDSCAPE_RIGHT</orientation> | |
</orientations> | |
</iphone> | |
<property name="ti.android.threadstacksize" type="int">65536</property> | |
<property name="ti.android.largeHeap" type="bool">true</property> | |
<property name="ti.android.fastdev" type="bool">false</property> | |
<android xmlns:android="http://schemas.android.com/apk/res/android"> | |
<manifest android:installLocation="auto"> | |
<activity android:alwaysRetainTaskState="true" | |
android:configChanges="keyboardHidden|orientation" | |
android:label="OnForce" android:name=".OnforceActivity" android:theme="@style/Theme.Titanium"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN"/> | |
<category android:name="android.intent.category.LAUNCHER"/> | |
</intent-filter> | |
</activity> | |
<supports-screens android:anyDensity="false"/> | |
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> | |
<uses-permission android:name="android.permission.CAMERA"/> | |
<uses-permission android:name="android.permission.FLASHLIGHT"/> | |
</manifest> | |
<services> | |
<service type="interval" url="testservice.js"/> | |
</services> | |
</android> | |
<mobileweb> | |
<precache/> | |
<splash> | |
<enabled>true</enabled> | |
<inline-css-images>true</inline-css-images> | |
</splash> | |
<theme>default</theme> | |
</mobileweb> | |
<modules> | |
<module platform="commonjs" version="2.0">ti.cloud</module> | |
<module platform="iphone" version="1.5.1">ti.styledlabel</module> | |
</modules> | |
</ti:app> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment