[TIMOB-24304] Android: Fetching Geolocation from background service causes crash on SDK > 22
GitHub Issue | n/a |
Type | Bug |
Priority | Critical |
Status | Closed |
Resolution | Fixed |
Resolution Date | 2017-02-16T00:28:48.000+0000 |
Affected Version/s | Release 6.0.1 |
Fix Version/s | Release 6.0.2 |
Components | n/a |
Labels | n/a |
Reporter | Jonas Bjurhult |
Assignee | Gary Mathews |
Created | 2016-03-17T08:19:13.000+0000 |
Updated | 2017-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
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.
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:
Code has been added as requested, is this ticket to be reopened?
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
@Sharif AbuDarda please see Jorgen's example above in response to your original request.
Hi, what was the resolution for this? I am experiencing the same behaviour. Android 6, Titanium SDK 6.0.1
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) {
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
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.
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.
Has there been no progress on this yet?
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
6_0_X: https://github.com/appcelerator/titanium_mobile/pull/8800
master: https://github.com/appcelerator/titanium_mobile/pull/8819
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.
Fixes are verified in installed versions: SDK 6.1.0.v20170215164209 SDK 6.0.2.v20170215162535