deckameron / Ti.Android.Geofence

A Geofence solution for Appcelerator Titanium Android
Other
17 stars 5 forks source link

Context.startForegroundService() did not then call Service.startForeground #8

Open deckameron opened 5 years ago

deckameron commented 5 years ago
[ERROR] :  TiExceptionHandler: (main) [9051,29263]
Context.startForegroundService() did not then call Service.startForeground()
[ERROR] :  TiExceptionHandler:
[ERROR] :  TiExceptionHandler:
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2204)
[ERROR] :  TiExceptionHandler:
android.os.Handler.dispatchMessage(Handler.java:108)
[ERROR] :  TiExceptionHandler:     android.os.Looper.loop(Looper.java:166)
[ERROR] :  TiExceptionHandler:
android.app.ActivityThread.main(ActivityThread.java:7529)
[ERROR] :  TiExceptionHandler:     java.lang.reflect.Method.invoke(Native
Method)
[ERROR] :  TiExceptionHandler:
com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
[ERROR] :  TiExceptionHandler:
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

It is an Android issue! Checkout here.

deckameron commented 5 years ago

Hello @AppWerft! Do you know any solution for this issue? Thank you!

m1ga commented 4 years ago

Any info here? Just had this error with a HUAWEI phone. But on a Pixel 4 it is working fine. Looking at the Android issue it looks like it has to do with initialization order of the service

deckameron commented 4 years ago

Hi @m1ga, I haven't worked on the module for a while, so I will go back to it and try again. Thank you for the tip regarding the initialization order of the service! If you find any other information about this issue, please let me know. :-)

m1ga commented 4 years ago

It was mentioned in the linked ticket somewhere. But I didn't try it yet. I'll have a look at the module, too.

deckameron commented 4 years ago

This is the way! :-) I will have to refactor a lot my code to get this working. I will give it a try.

deckameron commented 4 years ago

@m1ga Could you please test this version? Let me know if the crash stops on the HUAWEI.

m1ga commented 4 years ago

now my phones (Samsung A5 Android 8) and Pixel 4 (Android 10) stop with this error:

[ERROR] TiExceptionHandler: (main) [23,4267] Unable to start receiver ti.android.geofence.GeofenceBroadcastReceiver: java.lang.IllegalArgumentException: No such service ComponentInfo{com.test.geo/ti.android.geofence.JavascriptService}
[ERROR] TiExceptionHandler:
[ERROR] TiExceptionHandler:     android.os.Parcel.readException(Parcel.java:1971)
[ERROR] TiExceptionHandler:     android.os.Parcel.readException(Parcel.java:1913)
[ERROR] TiExceptionHandler:     android.app.job.IJobScheduler$Stub$Proxy.enqueue(IJobScheduler.java:211)
[ERROR] TiExceptionHandler:     android.app.JobSchedulerImpl.enqueue(JobSchedulerImpl.java:53)
[ERROR] TiExceptionHandler:     android.support.v4.app.JobIntentService$JobWorkEnqueuer.enqueueWork(JobIntentService.java:342)
[ERROR] TiExceptionHandler:     android.support.v4.app.JobIntentService.enqueueWork(JobIntentService.java:522)
[ERROR] TiExceptionHandler:     android.support.v4.app.JobIntentService.enqueueWork(JobIntentService.java:500)
[ERROR] TiExceptionHandler:     ti.android.geofence.JavascriptService.enqueueWork(JavascriptService.java:24)
[ERROR] TiExceptionHandler:     ti.android.geofence.GeofenceBroadcastReceiver.onReceive(GeofenceBroadcastReceiver.java:28)
[ERROR] TiExceptionHandler:     android.app.ActivityThread.handleReceiver(ActivityThread.java:3392)
[ERROR] TiExceptionHandler:     android.app.ActivityThread.-wrap18(Unknown Source:0)
[ERROR] TiExceptionHandler:     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1780)
[ERROR] TiExceptionHandler:     android.os.Handler.dispatchMessage(Handler.java:105)
[ERROR] TiExceptionHandler:     android.os.Looper.loop(Looper.java:164)
[ERROR] TiExceptionHandler:     android.app.ActivityThread.main(ActivityThread.java:6944)
[ERROR] TiExceptionHandler:     java.lang.reflect.Method.invoke(Native Method)
[ERROR] TiExceptionHandler:     com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
[ERROR] TiExceptionHandler:     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
m1ga commented 4 years ago

https://stackoverflow.com/a/49418249/5193915

Your app will crash if you call Context.startForegroundService(...) and then call Context.stopService(...) before Service.startForeground(...) is called.

https://github.com/deckameron/Ti.Android.Geofence/blob/0fef759e69aaa1a89a42f7daec2647f6d4de22b5/android/src/ti/android/geofence/JavascriptService.java#L63

https://github.com/deckameron/Ti.Android.Geofence/blob/0fef759e69aaa1a89a42f7daec2647f6d4de22b5/android/src/ti/android/geofence/JavascriptService.java#L80

not tested yet. Will check it at the weekend

deckameron commented 4 years ago

Thank you @m1ga ! Your help is greatly appreciated.

deckameron commented 4 years ago

Hi @m1ga! I managed to get it working on my Pixel with Android 10. I have uploaded a newer version. Please, feel free to give it a try whenever possible. :-)

m1ga commented 4 years ago

it still crashes for me after 15 secs:

[INFO]  entered
[INFO]  JavascriptService: Service Started
[WARN]  TiJSIntervalService: (main) [1,5373] The intent is missing the extra value 'interval', therefore the code will be executed only once.
[INFO]  IT WORKED! It is a service
[INFO]  @@@ Service started.
[INFO]  id region1
[INFO]  trigger from service
[INFO]  GeofenceModule: (AsyncTask #1) [5,5378] Unable to find image [null] from assets.
[INFO]  I/com.test.geo: Thread[6,tid=22279,WaitingInMainSignalCatcherLoop,Thread*=0x784cc37000,peer=0x14240220,"Signal Catcher"]: reacting to signal 3
[INFO]  I/com.test.geo:
[INFO]  I/com.test.geo: Wrote stack traces to tombstoned
[DEBUG] AndroidRuntime: Shutting down VM
[ERROR] TiExceptionHandler: (main) [14885,20263] Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{bbaff86 u0 com.test.geo/.MyServiceService}
[ERROR] TiExceptionHandler:
[ERROR] TiExceptionHandler:     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1945)
[ERROR] TiExceptionHandler:     android.os.Handler.dispatchMessage(Handler.java:107)
[ERROR] TiExceptionHandler:     android.os.Looper.loop(Looper.java:214)
[ERROR] TiExceptionHandler:     android.app.ActivityThread.main(ActivityThread.java:7356)
[ERROR] TiExceptionHandler:     java.lang.reflect.Method.invoke(Native Method)
[ERROR] TiExceptionHandler:     com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
[ERROR] TiExceptionHandler:     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

it will fire once for my current fence and it will show the trigger from service from the attached service. But if I wait 15 secs and do nothing (not chanigng my location) it will crash

var geofence = require("ti.android.geofence");

function addFences() {
    geofence.addGeofences({
        clearExistingFences: true,
        fences: [{
            "id": "region1",
            latitude: xxxx,
            longitude: xxxx,
            "radius": 100,
            "transitions": [
                geofence.GEOFENCE_TRANSITION_ENTER
            ],
            "title": "region1",
            "sound": "notification",
            "accentColor": "#E65100",
            "type": geofence.TYPE_PLACE_FENCE
        }],
        service: "com.test.geo.MyServiceService"
    });

    geofence.addEventListener(geofence.ERROR, function(e) {
        Ti.API.error(e.message);
    });

    geofence.addEventListener(geofence.ENTERED, function(e) {
        console.log("entered");
    });

    geofence.addEventListener(geofence.EXITED, function(e) {
        console.log("exited");
    });

    geofence.addEventListener(geofence.DWELL, function(e) {
        console.log("dwell");
    });

    geofence.addEventListener(geofence.STARTED_MONITORING, function(e) {
        console.log("start");
    });
}

function onOpen(e) {
    if (!Ti.Geolocation.hasLocationPermissions(Ti.Geolocation.AUTHORIZATION_ALWAYS)) {
        Ti.Geolocation.requestLocationPermissions(Ti.Geolocation.AUTHORIZATION_ALWAYS, function(e) {
            if (!e.success) {
                return;
            }
            console.log("got permission");
            addFences();
        });
    } else {
        console.log("got permission");
        addFences();
    }
}

function onClick(e) {
    geofence.startMonitoring();
}
function onClose(e){}
$.index.open();
<Alloy>
    <Window class="container" onOpen="onOpen">
        <Button id="btn" title="run" onClick="onClick"/>
    </Window>
</Alloy>

Edit: I've added a log inside onStartCommand and it looks like that it is not called at all.