Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-24304] Android: Fetching Geolocation from background service causes crash on SDK > 22

GitHub Issuen/a
TypeBug
PriorityCritical
StatusClosed
ResolutionFixed
Resolution Date2017-02-16T00:28:48.000+0000
Affected Version/sRelease 6.0.1
Fix Version/sRelease 6.0.2
Componentsn/a
Labelsn/a
ReporterJonas Bjurhult
AssigneeGary Mathews
Created2016-03-17T08:19:13.000+0000
Updated2017-02-16T02:02:29.000+0000

Description

File: android/modules/geolocation/src/java/ti/modules/titanium/geolocation/GeolocationModule.java Method: hasLocationPermissions() The method checks permission on the current Activity, but it might be null when running in a service. One solution is to change Activity currentActivity = TiApplication.getInstance().getCurrentActivity(); if (currentActivity.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { To Context context = TiApplication.getInstance().getApplicationContext(); if (context.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

Attachments

FileDateSize
backgroundService.zip2017-01-12T18:55:07.000+00003737387

Comments

  1. Sharif AbuDarda 2016-03-18

    Hello, Please provide a sample test code to test. It will be helpful to validate the issue if we are also able to regenerate the issue in our environment. Please provide a sample regeneratable code. and also steps to follow. Thanks.
  2. Jonas Bjurhult 2016-03-31

    Steps to follow: 1. Run the code below: 2. Make sure the main app is killed by force closing. 3. The service should start after the service interval. This will make the service crash since Activity is not accessible from the service once the main Activity has been killed. Example code: File: myservice.js: var service = Titanium.Android.currentService; var intent = service.intent; Ti.Geolocation.getCurrentPosition(function(e) {}); // This will make File: tiapp.xml: http://schemas.android.com/apk/res/android"> File: app.js: var intent = null, service = null; intent = Titanium.Android.createServiceIntent({ url : 'myservice.js', startMode : Titanium.Android.START_STICKY }); intent.putExtra('interval', 10000); if(!Titanium.Android.isServiceRunning(intent)) { service = Titanium.Android.startService(intent); }
  3. Adam Paxton 2016-04-05

    Code has been added as requested, is this ticket to be reopened?
  4. Jörgen Buder 2016-04-06

    Hi Ok, great, can you please give us the PR so we can track when the change is out for us to use in the SDK? /Jörgen
  5. Adam Paxton 2016-04-06

    @Sharif AbuDarda please see Jorgen's example above in response to your original request.
  6. sulaimaanrawoot 2017-01-03

    Hi, what was the resolution for this? I am experiencing the same behaviour. Android 6, Titanium SDK 6.0.1
  7. Jonas Bjurhult 2017-01-03

    The problem is that an Activity might not be present in the service. I patched GeolocationModule.java and switched Activity->Context, like this: \- Activity currentActivity = TiApplication.getInstance().getCurrentActivity(); \- if (currentActivity.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { + Context context = TiApplication.getInstance().getApplicationContext(); + if (context.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
  8. Sharif AbuDarda 2017-01-04

    Hello, Starting from Android 6.0 (API level 23), users need to grant certain permissions to apps while the app is running. "android.permission.ACCESS_FINE_LOCATION" is one of them. You can read it more [here](https://developer.android.com/training/permissions/requesting.html). http://docs.appcelerator.com/platform/latest/#!/api/Titanium.Android
  9. sulaimaanrawoot 2017-01-05

    Hi, I get that but when I check if the user has the permission or I request the permission in the background service then it throws the error.
  10. Sharif AbuDarda 2017-01-12

    I can verify the issue in SDK 6.0.1.GA. In Android 6.0.1 device. I am using the attached project. Below is the steps. On first build on device. 1.Clicked on start. location service permission popup appears. Clicked on "allow". 2.Put the app on background. There is no error. Also, the service is not invoked. 3.Put the app on foreground, and Clicked on start, the service started and Hits location. 4.Put the app on background. There is the error. Now, If I rebuild on the same device again. 1.Clicked on start, no location service permission popup appears now. 2.The service invoked. Put the app on background. There is the error. Thanks.
  11. sulaimaanrawoot 2017-01-25

    Has there been no progress on this yet?
  12. Jonas Bjurhult 2017-01-30

    Same problem found in: android/modules/android/src/java/ti/modules/titanium/android/AndroidModule.java android/modules/platform/src/java/ti/modules/titanium/platform/PlatformModule.java
  13. Gary Mathews 2017-02-01

    6_0_X: https://github.com/appcelerator/titanium_mobile/pull/8800
  14. Gary Mathews 2017-02-08

    master: https://github.com/appcelerator/titanium_mobile/pull/8819
  15. Abir Mukherjee 2017-02-16

    NPM Version: 2.15.9 Node Version: 4.5.0 Mac OS: 10.12.1 Appc CLI: 6.1.0 Appc CLI NPM: 4.2.8 Appcelerator Studio, build: 4.8.1.201612050850 Android Device 6.0.1 SDK 6.0.2 and 6.1.0 FR Passed. I followed the steps in the instruction detailed in the ticket. After launching the app, I clicked on "Start", and then put the app in the background. Then I brought the app in the foreground and clicked "Start" again. I then put the app in the background. No errors were seen during the test.
  16. Abir Mukherjee 2017-02-16

    Fixes are verified in installed versions: SDK 6.1.0.v20170215164209 SDK 6.0.2.v20170215162535

JSON Source