{ "id": "122288", "key": "AC-2302", "fields": { "issuetype": { "id": "1", "description": "A problem which impairs or prevents the functions of the product.", "name": "Bug", "subtask": false }, "project": { "id": "12217", "key": "AC", "name": "Appcelerator - INBOX", "projectCategory": { "id": "10000", "description": "", "name": "Customer Service" } }, "resolution": { "id": "5", "description": "All attempts at reproducing this issue failed, or not enough information was available to reproduce the issue. Reading the code produces no clues as to why this behavior would occur. If more information appears later, please reopen the issue.", "name": "Cannot Reproduce" }, "resolutiondate": "2014-07-09T22:17:15.000+0000", "created": "2013-11-11T14:27:01.000+0000", "labels": [], "versions": [], "issuelinks": [], "assignee": { "name": "mpmiranda", "key": "mpmiranda", "displayName": "Mauro Parra-Miranda", "active": true, "timeZone": "America/Mexico_City" }, "updated": "2016-03-08T07:41:18.000+0000", "status": { "description": "The issue is considered finished, the resolution is correct. Issues which are closed can be reopened.", "name": "Closed", "id": "6", "statusCategory": { "id": 3, "key": "done", "colorName": "green", "name": "Done" } }, "components": [ { "id": "14548", "name": "Titanium SDK & CLI", "description": "Please enter tickets related to the MobileSDK here." } ], "description": "I want to schedule a new notification through a serviceIntent, but i already have scheduled one. So first I stop the previous one and check if it has been stopped, when the previous serviceIntent has been stopped I start a new serviceIntent. \r\n\r\nBut when I start the new service, the previous one will be triggered immediately. What I am expecting is that the previous serviceIntent does not exist anymore.\r\n\r\nbelow an example of the problem: \r\n\r\n{code:title=app.js|borderStyle=solid}\r\nvar win = Titanium.UI.createWindow({\r\n\tbackgroundColor : 'red'\r\n});\r\nvar btn = Ti.UI.createButton({\r\n\ttitle : 'Add Notification'\r\n});\r\n\r\nbtn.addEventListener('click', function(e) {\r\n\tvar now = new Date().getTime();\r\n\tvar delta = new Date( now + (30 * 1000) );\r\n\tvar deltaMS = delta - now;\r\n\r\n\tvar intent = Ti.Android.createServiceIntent({\r\n\t\turl : 'ExampleService.js'\r\n\t});\r\n\tintent.putExtra('interval', deltaMS);\r\n\tintent.putExtra('message' , 'This is that little extra');\r\n\tif (Ti.Android.isServiceRunning(intent)){\r\n\t\tTi.API.log(\"service is running, stopping it now\");\r\n\t\tTi.Android.stopService(intent);\r\n\t}\r\n\tif (Ti.Android.isServiceRunning(intent) == false){\r\n\t\tTi.API.log(\"service is NOT running, starting it now\");\r\n\t\tTi.Android.startService(intent);\r\n\t}\r\n\t\r\n});\r\nwin.add(btn);\r\nwin.open();\r\n{code} ", "attachment": [], "flagged": false, "summary": "Android: Ti.Android.stopService(intent) is not removing the service", "creator": { "name": "nsttu", "key": "nsttu", "displayName": "Barry van der Pol", "active": true, "timeZone": "Europe/Berlin" }, "subtasks": [], "reporter": { "name": "nsttu", "key": "nsttu", "displayName": "Barry van der Pol", "active": true, "timeZone": "Europe/Berlin" }, "environment": "Android 4.3", "comment": { "comments": [ { "id": "280222", "author": { "name": "ragrawal", "key": "ragrawal", "displayName": "Ritu Agrawal", "active": true, "timeZone": "America/Los_Angeles" }, "body": "Hi Barry,\r\n\r\nPlease try the code below. The logic seems to be incorrect. You need to initialize the intent variable first.\r\n{code}\r\nvar win = Titanium.UI.createWindow({\r\n backgroundColor : 'red'\r\n});\r\nvar btn = Ti.UI.createButton({\r\n title : 'Add Notification'\r\n});\r\n \r\nvar intent = null;\r\nbtn.addEventListener('click', function(e) {\r\n if (intent){\r\n\t\tif (Ti.Android.isServiceRunning(intent)){\r\n\t Ti.API.log(\"service is running, stopping it now\");\r\n\t Ti.Android.stopService(intent);\r\n\t }else{\r\n\t \tTi.API.log(\"service is NOT running, starting it now\");\r\n\t \tTi.Android.startService(intent);\r\n\t } \t\r\n }else{\r\n \tvar now = new Date().getTime();\r\n\t var delta = new Date( now + (30 * 1000) );\r\n\t var deltaMS = delta - now;\r\n\t \r\n\t intent = Ti.Android.createServiceIntent({\r\n\t url : 'ExampleService.js'\r\n\t });\r\n\t intent.putExtra('interval', deltaMS);\r\n\t intent.putExtra('message' , 'This is that little extra');\r\n Ti.Android.startService(intent);\r\n }\r\n \r\n // if (Ti.Android.isServiceRunning(intent) == false){\r\n // Ti.API.log(\"service is NOT running, starting it now\");\r\n// \r\n // }\r\n \r\n});\r\nwin.add(btn);\r\nwin.open();  \r\n{code}\r\n\r\nLets us know if this works. We will be glad to hear from you.\r\n", "updateAuthor": { "name": "shossain", "key": "shossain", "displayName": "Shak Hossain", "active": false, "timeZone": "America/Los_Angeles" }, "created": "2013-11-19T20:45:36.000+0000", "updated": "2013-12-06T18:56:51.000+0000" }, { "id": "280526", "author": { "name": "nsttu", "key": "nsttu", "displayName": "Barry van der Pol", "active": true, "timeZone": "Europe/Berlin" }, "body": "Hello Ritu,\n\nThanks for your reply. I have tried the code you posted above, but the problem still occurs.\nWhen the button is pressed once, de service will start with a new created intent.\nWhen the button is pressed twice the service will be stoppped.\nBut when the button is pressed for the third time the problem occurs, the service with the intent that started when the button was pressed for the first time will immediately be triggered. What I am expecting is that is the service will restart. In case that a new intent is provided to the startService method, it will never be triggered. \n\nbellow i have changed the code you have provided where it will put an new intent to the service.\n\nvar win = Titanium.UI.createWindow({\n\tbackgroundColor : 'red'\n});\nvar btn = Ti.UI.createButton({\n\ttitle : 'Add Notification'\n});\n\nvar intent = null;\nbtn.addEventListener('click', function(e) {\n\tif (intent){\n\t\tif (Ti.Android.isServiceRunning(intent)){ \n\t\t\tTi.API.log(\"service is running, stopping it now\"); \n\t\t\tTi.Android.stopService(intent);\n\t\t}else{\n\t\t\tvar now = new Date().getTime();\n\t\t\tvar delta = new Date( now + (30 * 1000) );\n\t\t\tvar deltaMS = delta - now;\n\t\t\tintent = Ti.Android.createServiceIntent({ url : 'ExampleService.js' });\n\t\t\tintent.putExtra('interval', deltaMS);\n\t\t\tintent.putExtra('message' , 'This is the second or later intent');\n\t\t\tTi.API.log(\"service is NOT running, starting it now\");\n\t\t\tTi.Android.startService(intent);\n\t\t} \n\t}else{\n\t\tvar now = new Date().getTime();\n\t\tvar delta = new Date( now + (30 * 1000) );\n\t\tvar deltaMS = delta - now;\n\t\tintent = Ti.Android.createServiceIntent({ url : 'ExampleService.js' });\n\t\tintent.putExtra('interval', deltaMS);\n\t\tintent.putExtra('message' , 'This is the first intent');\n\t\tTi.Android.startService(intent);\n\t}\n// if (Ti.Android.isServiceRunning(intent) == false){ // Ti.API.log(\"service is NOT running, starting it now\"); // // }\n});\n\nwin.add(btn);\nwin.open();\n\nwhat will happen in this case is: \nfirst buttonpress: an service will start with an intent that contains an extra param 'message' with the value \"This is the first intent\".\nsecond buttonpress: will stop the service with the intent that contains an extra param 'message' with the value \"This is the first intent\".\nthird buttonpress: will execute ExampleService.js immediately with extra param 'message' with the value \"This is the first intent\". And the intent with extra param 'message' with the value \"This is the second or later intent\" will never be executed.\n\nI hope you understand the problem better now.\n\n\n", "updateAuthor": { "name": "nsttu", "key": "nsttu", "displayName": "Barry van der Pol", "active": true, "timeZone": "Europe/Berlin" }, "created": "2013-11-21T11:54:35.000+0000", "updated": "2013-11-21T11:54:35.000+0000" }, { "id": "283071", "author": { "name": "mrahman", "key": "mrahman", "displayName": "Mostafizur Rahman", "active": true, "timeZone": "Asia/Dhaka" }, "body": "Hi Barry,\r\n\r\nI tested the code below on an Android emulator. It is removing the service as expected. This is slightly modified code than the one you posted. Would you mind trying it out and let us know if this solves your issue. I appreciate your response.\r\n\r\napp.js\r\n\r\n{code}\r\nvar win = Titanium.UI.createWindow({\r\n\tbackgroundColor : 'red'\r\n});\r\nvar btn = Ti.UI.createButton({\r\n\ttitle : 'Add Notification'\r\n});\r\n\r\nvar intentStatus = false;\r\nbtn.addEventListener('click', function(e) {\r\n\r\n\tvar now = new Date().getTime();\r\n\tvar delta = new Date(now + (30 * 1000));\r\n\tvar deltaMS = delta - now;\r\n\tTi.API.info('deltaMS: ' + deltaMS);\r\n\tvar intent = Ti.Android.createServiceIntent({\r\n\t\turl : 'ExampleService.js'\r\n\t});\r\n\tif (Ti.Android.isServiceRunning(intent)) {\r\n\t\tTi.API.log(\"service is running, stopping it now\");\r\n\t\tTi.Android.stopService(intent);\r\n\t} else if (intentStatus) {\r\n\t\tintent.putExtra('interval', deltaMS);\r\n\t\tintent.putExtra('message', 'This is the second or later intent');\r\n\t\tTi.API.log(\"This is the second or later intent\");\r\n\t\tTi.Android.startService(intent);\r\n\t} else {\r\n\t\tintent.putExtra('interval', deltaMS);\r\n\t\tintent.putExtra('message', 'This is the first intent');\r\n\t\tTi.API.log(\"This is the first intent\");\r\n\t\tTi.Android.startService(intent);\r\n\r\n\t\tintentStatus = true;\r\n\t}\r\n\r\n});\r\n/*\r\n btn.addEventListener('click', function(e) {\r\n if (intent) {\r\n if (Ti.Android.isServiceRunning(intent)) {\r\n Ti.API.log(\"service is running, stopping it now\");\r\n Ti.Android.stopService(intent);\r\n } else {\r\n var now = new Date().getTime();\r\n var delta = new Date(now + (30 * 1000));\r\n var deltaMS = delta - now;\r\n intent = Ti.Android.createServiceIntent({\r\n url : 'ExampleService.js'\r\n });\r\n intent.putExtra('interval', deltaMS);\r\n intent.putExtra('message', 'This is the second or later intent');\r\n Ti.API.log(\"service is NOT running, starting it now\");\r\n Ti.Android.startService(intent);\r\n }\r\n } else {\r\n var now = new Date().getTime();\r\n var delta = new Date(now + (30 * 1000));\r\n var deltaMS = delta - now;\r\n intent = Ti.Android.createServiceIntent({\r\n url : 'ExampleService.js'\r\n });\r\n intent.putExtra('interval', deltaMS);\r\n intent.putExtra('message', 'This is the first intent');\r\n Ti.Android.startService(intent);\r\n }\r\n // if (Ti.Android.isServiceRunning(intent) == false){ // Ti.API.log(\"service is NOT running, starting it now\"); // // }\r\n }); */\r\n\r\nwin.add(btn);\r\nwin.open();\r\n\r\n{code}\r\n\r\nExampleService.js\r\n\r\n{code}\r\nvar service = Ti.Android.currentService;\r\nvar intent = service.getIntent();\r\nvar teststring = intent.getStringExtra('message') + ' (instance ' + service.serviceInstanceId + ')';\r\n\r\nTi.API.info('teststring: ' + teststring); \r\n{code}\r\n\r\ntiapp.xml\r\n{code}\r\n\r\n \r\n \r\n \r\n \r\n{code}\r\n\r\n\r\nThanks ", "updateAuthor": { "name": "shossain", "key": "shossain", "displayName": "Shak Hossain", "active": false, "timeZone": "America/Los_Angeles" }, "created": "2013-12-07T20:19:24.000+0000", "updated": "2013-12-08T05:44:22.000+0000" }, { "id": "311531", "author": { "name": "mpmiranda", "key": "mpmiranda", "displayName": "Mauro Parra-Miranda", "active": true, "timeZone": "America/Mexico_City" }, "body": "Hello [~nsttu]!\r\n\r\nDid the last comment fix your issue? \r\n\r\nLet us know! \r\n\r\nThanks!", "updateAuthor": { "name": "mpmiranda", "key": "mpmiranda", "displayName": "Mauro Parra-Miranda", "active": true, "timeZone": "America/Mexico_City" }, "created": "2014-06-30T01:04:05.000+0000", "updated": "2014-06-30T01:04:05.000+0000" }, { "id": "313097", "author": { "name": "mpmiranda", "key": "mpmiranda", "displayName": "Mauro Parra-Miranda", "active": true, "timeZone": "America/Mexico_City" }, "body": "This is working for [~mrahman], and customer hasn't replied to us yet.", "updateAuthor": { "name": "mpmiranda", "key": "mpmiranda", "displayName": "Mauro Parra-Miranda", "active": true, "timeZone": "America/Mexico_City" }, "created": "2014-07-09T22:17:15.000+0000", "updated": "2014-07-09T22:17:15.000+0000" } ], "maxResults": 9, "total": 9, "startAt": 0 } } }