Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-9831] Android: background services stop when the app is closed via the BACK button

GitHub Issuen/a
TypeBug
PriorityHigh
StatusClosed
ResolutionFixed
Resolution Date2012-08-15T11:03:26.000+0000
Affected Version/sRelease 2.1.0
Fix Version/sSprint 2012-15 Core, Release 2.1.2, Release 3.0.0
ComponentsAndroid
Labelscore, module_notification_manager, qe-testadded
ReporterDavide Cassenti
AssigneeBill Dawson
Created2012-06-28T08:51:04.000+0000
Updated2013-12-31T13:39:36.000+0000

Description

Problem description

A background service in Android stops running when the user exits the app using the BACK button. The same service works fine if the app is in foreground or in background (clicking the HOME button).

Steps to reproduce

- Create an app as explained in this GIST: https://gist.github.com/e309f6a56ea6cb1fd394 - Run the app on Android: as soon as the app starts, a notification is shown every 10 seconds After that, there are 3 cases: 1. Keep the app running: every 10 seconds a notification is shown (OK). 2. Put the app in background by clicking the HOME button: notifications keep showing (OK) 3. Click the BACK button (this closes the app): the background service is stopped and no more notifications are shown (BUG)

Expected behavior

Also in case #3, the notifications should keep running every 10 seconds.

Customer ticket

http://support.appcelerator.com/tickets/APP-829749

Comments

  1. Bill Dawson 2012-07-30

    Testing Notes

    * Create a new app and use the code in the Gist referred to in this item's description. That means including the notification.js and the section of the tiapp.xml, in addition to an app.js. But then replace the app.js with this code, which includes a way to stop the Service, which is needed in order to test that indeed the runtime still shuts down when no activities or services are running.
       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
       
       if (!Ti.Android.isServiceRunning(intent)) {
       	Titanium.Android.startService(intent);
       }
       
       var btnStop = Ti.UI.createButton({title: "Stop service"});
       win.add(btnStop);
       btnStop.addEventListener("click", function() {
       	Ti.Android.stopService(intent);
       });
       
       win.open();
       
    * Put this in your test app's tiapp.xml:
       <property type="bool" name="ti.android.debug">true</property>
       
    * Run the app on device or simulator. * When the app starts, you'll notice right away a notification in the notification area. Another is added each 10 seconds. You don't need to do anything with these notifications, just notice that they are there and happening each 10 seconds. * Back out of the app. * In the fail case, no more notifications will come in the 10-sec intervals. When testing the fix, they *should* continue to come. * When testing the fix: go back into the app after you are satisfied that the notifications are still coming in every 10 seconds. * Click the "Stop service" button. * Back out of the app. * Check logcat. You should see:
       D/KrollRuntime(xxxx): Disposing runtime.
       
    That proves that the runtime does indeed shutdown (which is good for memory) when there are no more running services or activities that need it.
  2. Bill Dawson 2012-07-30

    PR ready now: https://github.com/appcelerator/titanium_mobile/pull/2658
  3. Evgenii Tcarev 2012-08-14

    Verified fixed with: Titanium Studio, build: 2.1.1.201207271312 SDK version: 2.2.0.v20120813184911 Devices: LG VS910 4G (2.3.6) Samsung Galaxy Nexus (4.0.4)
  4. Allen Yeung 2012-08-15

    Reopen to update label

JSON Source