Red-Folder / bgs-core

Core code for the Cordova Background Service
Other
236 stars 105 forks source link

onStartCommand implementation #9

Closed mikedumont closed 9 years ago

mikedumont commented 10 years ago

Hello,

First of all thnx for the plugin, it works great, i don't really have a problem but i would like to share something with you and ask your opinion about my solution.

In my previous post on this repo i had a problem with the doWork method, it didn't run when i used onStartCommand in my backgroundservice. While I thought i solved the problem by reinstalling the plugin, it was only partly the sollution. I noticed yesterday that my backgroundservice started onboot as supposed to. But the timer didn´t start running.. Again the dowork and all the other methods didn´t get triggerd because of the onStartCommand...

So i started to digg around in the plugin and i noticed you used:

   public void onStart(Intent intent, int startId) {
    initialiseService();
}

If i'm correct this method is deprecated, so can't you replace this with the onStartCommand? Or use it next to the onStart method?

Because I think this is what causing my problem: I use onStartCommand in my bachgroundservice, I need to use the annotation Overridde. In the plugin their isn't an onStartcommand (only onStart) so the initialiseService method does'nt get triggerd. Because the onStartCommand doesn't override onStart (<--guessing here).

I tested and solved this by using an onStartcommand in the plugins BackGroundService.java like this:

  @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            initialiseService();
        return START_REDELIVER_INTENT;   //Could also be START_STICKY
    }

and this piece of code in my backgroundservice:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    handleOnStart(intent);
     Log.d("ONSTARTCOMMAND", "intent" + intent);
    return START_REDELIVER_INTENT;  
}

After doing this everything runs like a charm. What are your ideas on this subject? (i'm not even close to be a JAVA pro so correct me if i'm wrong)

Greetings Mike

Red-Folder commented 10 years ago

I'm certainly not a Java Pro - just someone with a bit of code that seems to help others - so we're in the same boat ;)

Bit thanks for taking the time and effort to explore and experiment with this issue. Most issues get logged with little explanation ... let alone an attempt to resolve. So thanks for that.

Where did you see that onStart is depreciated? I've had a look at the official docs page (http://developer.android.com/reference/android/app/Activity.html#onStart()) and I can't see anything about it being depreciated. One point I did notice is:

Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.

I'm not currently doing that, so I think I probably need to add that to the code (no idea if that would fix your issue).

I'm not very familiar with onStartCommand. I believe is called following onCreate - which already does the initialiseService()

Which Android version do you see this problem with?

mikedumont commented 10 years ago

Hello Mark,

Thank you for your response, i'm always interested in the way things work.

The onStart command was deprecated in api level 5, it is noted here (in the Public Methods table): http://developer.android.com/reference/android/app/Service.html#onStart(android.content.Intent,int)

I implemented a super class, but i did it for the onStartCommand. It did solve the issue for me:

        super.onStartCommand(intent, flags, startId);

I'm currently running Android version 4.3 Jelly Beans on a sumsung Galaxy S3. (I also tested the app with Android 2.3.3 Gingerbread with a HTC phone, wich ran just fine ecept for some minor stuff with some notifications).

lylepratt commented 9 years ago

I'm also interested in using onStartCommand, so that this can be used to start a Foreground service using startForeground in conjunction with a persistent Notification.

Red-Folder commented 9 years ago

Notes from review of http://developer.android.com/reference/android/app/Service.html#onStart(android.content.Intent,int)

Red-Folder commented 9 years ago

Tasks to do:

1) Migrate current onStart to onStartCommand - returning the START_STICKY 2) Check that all clean up activity will be performed 3) Run the doWork() in a separate thread (assuming I'm not already)

Red-Folder commented 9 years ago

Committed 1 & 2 above in commit e2eee2495f0e50b9968d7dc144559ffde69fb788

Red-Folder commented 9 years ago

Double checked that the doWork() in running in a separate (which it is). Runs as a separate TimerTask.

This issue considered closed (please log fresh issue if not working correctly)