Skip to content

Instantly share code, notes, and snippets.

@davidecassenti
Created June 28, 2012 15:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidecassenti/e309f6a56ea6cb1fd394 to your computer and use it in GitHub Desktop.
Save davidecassenti/e309f6a56ea6cb1fd394 to your computer and use it in GitHub Desktop.
Background Service problem win Android
This GIST contains a simple example of a Titanium app that runs a background service every 10 seconds. The service keeps working if the app is running, as well as the app is in background (click HOME button).
However, clicking BACK, the app is killed and the background service stop working. Based on the documentation on http://developer.appcelerator.com/doc/mobile/android-simple-services this is a bug. Under Features, I can in fact see:
"Can survive -- i.e., keep going -- even when you back out of an application."
NOTE: the notification.js file is saved under Resources/android
// create a window
var win = Ti.UI.createWindow({
backgroundColor: 'black'
});
var label = Ti.UI.createLabel({
text: 'A service is now running: you will see a notification every 10 seconds',
width: Ti.UI.SIZE,
top: 10,
color: 'white'
});
win.add(label);
// create the background service
var SECONDS = 10; // every 10 seconds
var intent = Titanium.Android.createServiceIntent({
url: 'notification.js'
});
intent.putExtra('interval', SECONDS * 1000); // Needs to be milliseconds
Titanium.Android.startService(intent);
win.open();
var intent = Ti.Android.createIntent({
flags : Ti.Android.FLAG_ACTIVITY_CLEAR_TOP | Ti.Android.FLAG_ACTIVITY_NEW_TASK,
className : 'com.appcelerator.testapp.TestappActivity'
});
intent.addCategory(Ti.Android.CATEGORY_LAUNCHER);
var pending = Titanium.Android.createPendingIntent({
intent: intent,
flags: Titanium.Android.FLAG_UPDATE_CURRENT
});
// create a random id
var nId = parseInt(10000 * Math.random());
// Create the notification
var notification = Titanium.Android.createNotification({
contentTitle: 'Notification #' + nId,
contentText : 'Click to return to the application.',
contentIntent: pending
});
// Send the notification.
Ti.Android.NotificationManager.notify(nId, notification);
<?xml version="1.0" encoding="UTF-8"?>
<ti:app xmlns:ti="http://ti.appcelerator.org">
<deployment-targets>
<target device="mobileweb">false</target>
<target device="iphone">false</target>
<target device="ipad">false</target>
<target device="android">true</target>
<target device="blackberry">false</target>
</deployment-targets>
<sdk-version>2.0.2.GA</sdk-version>
<id>com.appcelerator.testapp</id>
<name>TestApp</name>
<version>1.0</version>
<publisher>dcassenti</publisher>
<url>http://www.appcelerator.com</url>
<description>Just a test app</description>
<copyright>2012 by dcassenti</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>46cdc17b-ae0c-4df5-bbfb-4f2f416d4a02</guid>
<property name="ti.ui.defaultunit">system</property>
<android xmlns:android="http://schemas.android.com/apk/res/android">
<services>
<service url="notification.js" type="interval" />
</services>
</android>
</ti:app>
@khanhtran3005
Copy link

execuse me. I've just develop Android app using Titanium for one month. So, how is this problem? Have you fixed it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment