Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Run Background service Error #86

Closed
davidperafan opened this issue Oct 20, 2017 · 9 comments
Closed

Run Background service Error #86

davidperafan opened this issue Oct 20, 2017 · 9 comments
Assignees
Milestone

Comments

@davidperafan
Copy link

Which platform(s) does your issue occur on?

  • Android version
  • emulator and device

Please, provide the following version numbers that your issue occurs with:

  • CLI: 3.2.1
  • Cross-platform modules: 3.2.0
  • Runtime(s): "tns-android": {
    "version": "3.2.0"
    }
  • Plugin(s):
    "dependencies": {
    "buffer": "^5.0.8",
    "js-base64": "^2.3.2",
    "moment": "^2.19.1",
    "native-socket": "file:./files/Nativesocket",
    "nativescript-android-utils": "^1.0.2",
    "nativescript-bootreceiver": "^1.0.0",
    "nativescript-checkbox": "^3.0.1",
    "nativescript-couchbase": "^1.0.18",
    "nativescript-geolocation": "4.2.0",
    "nativescript-loading-indicator": "^2.4.0",
    "nativescript-localize": "^2.0.1",
    "nativescript-mapbox": "^3.1.2",
    "nativescript-permissions": "^1.2.3",
    "nativescript-pulltorefresh": "^2.0.2",
    "nativescript-secure-storage": "^2.2.1",
    "nativescript-theme-core": "~1.0.2",
    "nativescript-toast": "^1.4.6",
    "netchecksum": "^0.1.1",
    "timrjs": "^1.0.1",
    "tns-core-modules": "^3.2.0",
    "tns-ios": "3.2.0",
    "validator": "^8.2.0"
    },
    "devDependencies": {
    "awesome-typescript-loader": "~3.1.3",
    "babel-traverse": "6.25.0",
    "babel-types": "6.25.0",
    "babylon": "6.17.4",
    "copy-webpack-plugin": "~4.0.1",
    "extract-text-webpack-plugin": "^3.0.1",
    "lazy": "1.0.11",
    "nativescript-css-loader": "~0.26.0",
    "nativescript-dev-sass": "^1.3.2",
    "nativescript-dev-typescript": "^0.5.1",
    "nativescript-dev-webpack": "^0.8.0",
    "nativescript-worker-loader": "~0.8.1",
    "raw-loader": "~0.5.1",
    "resolve-url-loader": "^2.1.1",
    "tns-platform-declarations": "^3.1.1",
    "typescript": "^2.5.3",
    "webpack": "~3.2.0",
    "webpack-bundle-analyzer": "^2.8.2",
    "webpack-sources": "~1.0.1"
    },

Please, tell us how to recreate the issue in as much detail as possible.

I have a service background and call watch or get position, with version 3.0.1 of the plugin well ok, but whit minors errors, I'am test with versions 4.1.0 and 4.2.0 and
It works very well, so when app is open everything work ok, but when app is close and de service run on background mode i have a error, I'm using

android.app.Service.extend("com.tns.nativescript", {
onStartCommand: function(intent, flags, startId) {
this.super.onStartCommand(intent, flags, startId);
return android.app.Service.START_STICKY;
},
onCreate: function() {
watchLocation(); // here call wathc or get position
},
onBind: function(intent) {
console.log("on Bind Services");
},
onUnbind: function(intent){
console.log('UnBind Service');
},
onDestroy: function(){
console.log('service onDestroy);
}
});

Error:

Error: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager android.content.Context.getPackageManager()' on a null object reference
com.google.android.gms.common.zzo.isGooglePlayServicesAvailable(Unknown Source)
com.google.android.gms.common.zze.isGooglePlayServicesAvailable(Unknown Source)
com.google.android.gms.common.GoogleApiAvailability.isGooglePlayServicesAvailable(Unknown Source)
com.tns.Runtime.callJSMethodNative(Native Method)
com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1084)
com.tns.Runtime.callJSMethodImpl(Runtime.java:966)
com.tns.Runtime.callJSMethod(Runtime.java:953)
com.tns.Runtime.callJSMethod(Runtime.java:937)
com.tns.Runtime.callJSMethod(Runtime.java:929)
com.tns.gen.java.lang.Runnable.run(Runnable.java:10)
android.os.Handler.handleCallback(Handler.java:739)
android.os.Handler.dispatchMessage(Handler.java:95)
android.os.Looper.loop(Looper.java:148)
android.app.ActivityThread.main(ActivityThread.java:5417)
java.lang.reflect.Method.invoke(Native Method)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

@DimitarTachev
Copy link
Contributor

Hi @p3r4f089,

Thanks for reporting this exception!

I added a background service in the Demo app trying to reproduce the issue but everything is working as expected. The demo is now toasting the current location every 3 seconds even if the app is not active.

Could you check the differences between your code and the code in pull request #87 and update us back?

@davidperafan
Copy link
Author

when the app is running in the background works as expected, the problem starts when i kill the app , the service keeps running but the call to the location method gets an error as shown above.

@davidperafan
Copy link
Author

Hi @DimitarTachev I used the background service demo and it worked correctly, but when i try to get the position outside of the service it fails.

my code:

import * as geolocation from "nativescript-geolocation";
import { Accuracy } from "ui/enums";
import * as utils from "utils/utils";
declare let com: any;

var context = utils.ad.getApplicationContext();
var intent = new android.content.Intent(context, com.mc.TrackService.class);

let Toast = require("nativescript-toast");
let watchId;

(<any>android.app.Service).extend("com.mc.TrackService", {
    onStartCommand: function (intent, flags, startId) {
        this.super.onStartCommand(intent, flags, startId);
        return android.app.Service.START_STICKY;
    },
    onCreate: function () {
        geolocation.enableLocationRequest().then(function () {
            watchPosition();
        }, function (e) {
            console.log("Background enableLocationRequest error: " + (e.message || e));
        });
    },
    onBind: function (intent) {
        console.log("on Bind Services");
    },
    onUnbind: function(intent){
        console.log('UnBind Service');
    },
    onDestroy: function() {
        console.log('service onDestroy');
        clearWatch();
    }
});

function getCurrent(){
    geolocation.getCurrentLocation({
        desiredAccuracy: Accuracy.high,
        maximumAge: 5000,
        timeout: 10000
    })
        .then(function (loc) {
            if (loc) {
                console.log('My CurrentPosition :D ' + loc);
            }
        }, function (e) {
            console.error("Error: " + (e.message || e));
        });
}


function watchPosition(){
    watchId = geolocation.watchLocation(
        function (loc) {
            if (loc) {
                let toast = Toast.makeText('Background Location: ' + loc.latitude + ' ' + loc.longitude);
                toast.show();
                console.log('Background Location: ' + loc.latitude + ' ' + loc.longitude);
            }
        },
        function (e) {
            console.error("Background watchLocation error: " + (e.message || e));
        },
        {
            desiredAccuracy: Accuracy.high,
            updateDistance: 0.1,
            updateTime: 3000,
            minimumUpdateTime: 100
        });
}

function clearWatch(){
    if(watchId != null){
        geolocation.clearWatch(watchId);
    }
    
}

and Error

captura de pantalla 2017-11-02 a la s 11 55 12 a m

@agrw
Copy link

agrw commented Nov 2, 2017

getting same error, some google play service available check messed up??

Error: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager android.content.Context.getPackageManager()' on a null object reference
com.google.android.gms.common.zzo.isGooglePlayServicesAvailable(Unknown Source)
com.google.android.gms.common.zze.isGooglePlayServicesAvailable(Unknown Source)
com.google.android.gms.common.GoogleApiAvailability.isGooglePlayServicesAvailable(Unknown Source)
com.tns.Runtime.callJSMethodNative(Native Method)
com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1084)
com.tns.Runtime.callJSMethodImpl(Runtime.java:966)
com.tns.Runtime.callJSMethod(Runtime.java:953)
com.tns.Runtime.callJSMethod(Runtime.java:937)
com.tns.Runtime.callJSMethod(Runtime.java:929)
com.tns.gen.java.lang.Runnable.run(Runnable.java:10)
android.os.Handler.handleCallback(Handler.java:739)
android.os.Handler.dispatchMessage(Handler.java:95)
android.os.Looper.loop(Looper.java:148)
android.app.ActivityThread.main(ActivityThread.java:7325)
java.lang.reflect.Method.invoke(Native Method)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

@agrw
Copy link

agrw commented Nov 5, 2017

For nativescript-gelocation plugin, if I change line 194 in file geolocation.android.js from
var resultCode = GoogleApiAvailability.isGooglePlayServicesAvailable( application_1.android.foregroundActivity);
to
var resultCode = googleApiAvailability.isGooglePlayServicesAvailable(application_1.android.context);

Crash does not occur, the "application_1.android.foregroundActivitity" is not defined for background services? Whereas application_1.android.context is always defined.
Is this fix correct?

@zbranzov
Copy link
Contributor

zbranzov commented Nov 8, 2017

@p3r4f089 Using the demo app I'm unable to reproduce the error. I'm getting position using the Get Current Location button which is outside the service. Can you elaborate a bit more and provide a complete example? Maybe I miss something.

@agrw
Copy link

agrw commented Nov 9, 2017

I'm getting the current location inside the service, the variable application_1.android.foregroundActivity is null inside the service, but it you use application_1.android.context instead in call to googleApiAvailability.isGooglePlayServicesAvailable everything works. on line 194 in geolocation.android.js file. With this change everything works fine inside service or in foreground of app.

@Dragons0458
Copy link

@agrw Thank you! This works! Administrators must upload this change to the repository

@radeva radeva added this to the 3.4 milestone Dec 4, 2017
lini added a commit that referenced this issue Dec 4, 2017
- activate background monitoring with a button instead of on startup
- apply fix suggested in #86
lini added a commit that referenced this issue Dec 4, 2017
- activate background monitoring with a button instead of on startup
- apply fix suggested in #86
@lini
Copy link
Contributor

lini commented Dec 6, 2017

foregroundActivity -> context fix is published in v4.2.1

@lini lini closed this as completed Dec 6, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants