Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Service(timer task) not started when the application close #23

Closed
aravintha opened this issue Mar 11, 2015 · 6 comments
Closed

Service(timer task) not started when the application close #23

aravintha opened this issue Mar 11, 2015 · 6 comments
Labels

Comments

@aravintha
Copy link

In my cordova application enable the timer when the app is open the service run without any problem..when close the app the service never run.
cordova version 4.2 (and also tested in 3.3) and android version 4.4.2(nexus 7 device).

manifest is like:

    <receiver android:name="com.red_folder.phonegap.plugin.backgroundservice.BootReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

    <service android:name="com.example.hello.MyService" >
        <intent-filter>
            <action android:name="com.example.hello.MyService" />
        </intent-filter>
    </service>

but viewing in apps -> running apps it shows 0 process and 1 service.

Here is the logcat when close the app:

D/CordovaActivity(4246): CordovaActivity.onDestroy()
D/CordovaWebView(4246): >>> loadUrlNow()
D/BackgroundServicePluginLogic(4246): On Destroy Start
D/BackgroundServicePluginLogic(4246): Checking for services
D/BackgroundServicePluginLogic(4246): Found services
D/BackgroundServicePluginLogic(4246): Calling service.close()
D/ServiceDetails(4246): Close called
D/ServiceDetails(4246): Listener deregistering
D/ServiceDetails(4246): Listener closing
D/BackgroundServicePlugin(4246): Starting closeListener
D/BackgroundServicePlugin(4246): Sending update
D/BackgroundServicePlugin(4246): Start of transformResult
D/BackgroundServicePlugin(4246): Status is OK
D/BackgroundServicePlugin(4246): We have data
D/BackgroundServicePlugin(4246): End of transformResult
D/BackgroundServicePlugin(4246): Sent update
D/BackgroundServicePlugin(4246): Finished closeListener
D/ServiceDetails(4246): Listener closed
D/ServiceDetails(4246): Listener deregistered
D/ServiceDetails(4246): Removing ServiceListener
D/BackgroundService(4246): Listener removed
D/ServiceDetails(4246): Removing ServiceConnection
D/ServiceDetails(4246): Close finished
D/BackgroundServicePluginLogic(4246): On Destroy Finish

@opcitsistemas
Copy link

Same issue, i notice that if i close the app the service stop logging, in the running apps in the device i see the app but with 0 process and 1 service

Phonegap 4.2.0-0.23.0
Android Device 4.4.2

@aravintha aravintha changed the title Service not started when the application close Service(timer task) not started when the application close Mar 12, 2015
@Red-Folder
Copy link
Owner

Does the service ever start?

The service should show in logcat as "MyService". I'm not seeing it in the above logcat - this maybe because the service hasn't started or you've filtered out of the logcat.

If in doubt, post a full logcat of start, run, stop (full lifecycle)

@aravintha
Copy link
Author

Service is started ..timer task run every time interval when the app is open....if i close the app then timer task never run(for checking used the notificaton in doWork()) ....the above one is the stacktrace in my app..if you want i will send my entire application..

@Red-Folder
Copy link
Owner

In your logcat, you should have various logs Tag'ed as "Background Service"

You should see the Background Service giving messages like "Timer task starting work" & "Timer task completing work". This the code that wraps the doWork.

Are you seeing these message in your logcat when the app is open? Are you seeing these message in your logcat when the app is closed?

(If in doubt, post the whole logcat)

@aravintha
Copy link
Author

Logcat(When starting the service)

D/BackgroundServicePluginLogic(16652): Start of Execute
D/BackgroundServicePluginLogic(16652): Withing try block
D/BackgroundServicePluginLogic(16652): Finding servicename com.processdrive.novema.NovemaStagingBackupService
D/BackgroundServicePluginLogic(16652): Services contain 0 records
D/BackgroundServicePluginLogic(16652): Creating new Service Details
D/BackgroundServicePluginLogic(16652): Action = getStatus
D/BackgroundServicePlugin(16652): logicResult = com.red_folder.phonegap.plugin.backgroundservice.BackgroundServicePluginLogic$ExecuteResult@41e66c40
D/BackgroundServicePlugin(16652): Start of transformResult
D/BackgroundServicePlugin(16652): Status is OK
D/BackgroundServicePlugin(16652): We have data
D/BackgroundServicePlugin(16652): End of transformResult
D/BackgroundServicePlugin(16652): pluginResult = org.apache.cordova.PluginResult@41e6f378
D/BackgroundServicePlugin(16652): pluginResult.getMessage() = {"ErrorCode":0,"Success":true,"RegisteredForUpdates":false,"RegisteredForBootStart":false,"ErrorMessage":"","ServiceRunning":false}
D/BackgroundServicePlugin(16652): Dont keep Callback
D/BackgroundServicePluginLogic(16652): Start of Execute
D/BackgroundServicePluginLogic(16652): Withing try block
D/BackgroundServicePluginLogic(16652): Finding servicename com.processdrive.novema.NovemaStagingBackupService
D/BackgroundServicePluginLogic(16652): Services contain 1 records
D/BackgroundServicePluginLogic(16652): Found existing Service Details
D/BackgroundServicePluginLogic(16652): Action = startService
D/ServiceDetails(16652): Starting startService
D/ServiceDetails(16652): Attempting to bind to Service
D/ServiceDetails(16652): Starting bindToService
D/ServiceDetails(16652): Attempting to start service
W/ContextImpl(16652): Implicit intents with startService are not safe: Intent { act=com.processdrive.novema.NovemaStagingBackupService } android.content.ContextWrapper.startService:494 com.red_folder.phonegap.plugin.backgroundservice.BackgroundServicePluginLogic$ServiceDetails.bindToService:604 com.red_folder.phonegap.plugin.backgroundservice.BackgroundServicePluginLogic$ServiceDetails.startService:335
D/ServiceDetails(16652): Attempting to bind to service
W/ContextImpl(16652): Implicit intents with startService are not safe: Intent { act=com.processdrive.novema.NovemaStagingBackupService } android.content.ContextWrapper.bindService:517 com.red_folder.phonegap.plugin.backgroundservice.BackgroundServicePluginLogic$ServiceDetails.bindToService:607 com.red_folder.phonegap.plugin.backgroundservice.BackgroundServicePluginLogic$ServiceDetails.startService:335
D/ServiceDetails(16652): Waiting for service connected lock
I/BackgroundService(16652): Service creating
I/BackgroundService(16652): Initialising the service
I/BackgroundService(16652): Syncing result
I/BackgroundService(16652): Service started
I/BackgroundService(16652): onBind called
D/BackgroundService(16652): Listener added
D/ServiceDetails(16652): Finished bindToService
D/ServiceDetails(16652): Bind worked
D/ServiceDetails(16652): Finished startService
D/BackgroundServicePlugin(16652): logicResult = com.red_folder.phonegap.plugin.backgroundservice.BackgroundServicePluginLogic$ExecuteResult@41e58488
D/BackgroundServicePlugin(16652): Start of transformResult
D/BackgroundServicePlugin(16652): Status is OK
D/BackgroundServicePlugin(16652): We have data
D/BackgroundServicePlugin(16652): End of transformResult
D/BackgroundServicePlugin(16652): pluginResult = org.apache.cordova.PluginResult@41e74750
D/BackgroundServicePlugin(16652): pluginResult.getMessage() = {"TimerMilliseconds":60000,"LatestResult":{},"ErrorMessage":"","Configuration":{},"ErrorCode":0,"Success":true,"RegisteredForUpdates":false,"RegisteredForBootStart":false,"TimerEnabled":true,"ServiceRunning":true}
D/BackgroundServicePlugin(16652): Dont keep Callback
D/CordovaActivity(16652): onMessage(spinner,stop)
D/BackgroundServicePluginLogic(16652): Start of Execute
D/BackgroundServicePluginLogic(16652): Withing try block
D/BackgroundServicePluginLogic(16652): Finding servicename com.processdrive.novema.NovemaStagingBackupService
D/BackgroundServicePluginLogic(16652): Services contain 1 records
D/BackgroundServicePluginLogic(16652): Found existing Service Details
D/BackgroundServicePluginLogic(16652): Action = registerForUpdates
D/BackgroundServicePlugin(16652): logicResult = com.red_folder.phonegap.plugin.backgroundservice.BackgroundServicePluginLogic$ExecuteResult@41e76008
D/BackgroundServicePlugin(16652): Start of transformResult
D/BackgroundServicePlugin(16652): Status is OK
D/BackgroundServicePlugin(16652): We have data
D/BackgroundServicePlugin(16652): Keep Callback set to true
D/BackgroundServicePlugin(16652): End of transformResult
D/BackgroundServicePlugin(16652): pluginResult = org.apache.cordova.PluginResult@41e7c500
D/BackgroundServicePlugin(16652): pluginResult.getMessage() = {"TimerMilliseconds":60000,"LatestResult":{},"ErrorMessage":"","Configuration":{},"ErrorCode":0,"Success":true,"RegisteredForUpdates":true,"RegisteredForBootStart":false,"TimerEnabled":true,"ServiceRunning":true}
D/BackgroundServicePlugin(16652): Keep Callback
I/BackgroundService(16652): Timer task starting work
D/BackgroundService(16652): Is the service paused?
D/BackgroundService(16652): Service is not paused

I/BackgroundService(16652): Syncing result
I/BackgroundService(16652): Sending to all listeners
D/ServiceDetails(16652): Latest results received
D/ServiceDetails(16652): Calling listener
D/BackgroundServicePlugin(16652): Starting handleUpdate
D/BackgroundServicePlugin(16652): Sending update
D/BackgroundServicePlugin(16652): Start of transformResult
D/BackgroundServicePlugin(16652): Status is OK
D/BackgroundServicePlugin(16652): We have data
D/BackgroundServicePlugin(16652): Keep Callback set to true
D/BackgroundServicePlugin(16652): End of transformResult
D/BackgroundServicePlugin(16652): Sent update
D/BackgroundServicePlugin(16652): Finished handleUpdate
D/ServiceDetails(16652): Listener finished
I/BackgroundService(16652): Sent listener - 0
I/BackgroundService(16652): Timer task completing work

Timer task start work(when the app is open)

I/BackgroundService(16652): Timer task starting work
D/BackgroundService(16652): Is the service paused?
D/BackgroundService(16652): Service is not paused

I/BackgroundService(16652): Syncing result
I/BackgroundService(16652): Sending to all listeners
D/ServiceDetails(16652): Latest results received
D/ServiceDetails(16652): Calling listener
D/BackgroundServicePlugin(16652): Starting handleUpdate
D/BackgroundServicePlugin(16652): Sending update
D/BackgroundServicePlugin(16652): Start of transformResult
D/BackgroundServicePlugin(16652): Status is OK
D/BackgroundServicePlugin(16652): We have data
D/BackgroundServicePlugin(16652): Keep Callback set to true
D/BackgroundServicePlugin(16652): End of transformResult
D/BackgroundServicePlugin(16652): Sent update
D/BackgroundServicePlugin(16652): Finished handleUpdate
D/ServiceDetails(16652): Listener finished
I/BackgroundService(16652): Sent listener - 0
I/BackgroundService(16652): Timer task completing work

when close the app

D/BackgroundServicePluginLogic(16652): On Destroy Start
D/BackgroundServicePluginLogic(16652): Checking for services
D/BackgroundServicePluginLogic(16652): Found services
D/BackgroundServicePluginLogic(16652): Calling service.close()
D/ServiceDetails(16652): Close called
D/ServiceDetails(16652): Listener deregistering
D/ServiceDetails(16652): Listener closing
D/BackgroundServicePlugin(16652): Starting closeListener
D/BackgroundServicePlugin(16652): Sending update
D/BackgroundServicePlugin(16652): Start of transformResult
D/BackgroundServicePlugin(16652): Status is OK
D/BackgroundServicePlugin(16652): We have data
D/BackgroundServicePlugin(16652): End of transformResult
D/BackgroundServicePlugin(16652): Sent update
D/BackgroundServicePlugin(16652): Finished closeListener
D/ServiceDetails(16652): Listener closed
D/ServiceDetails(16652): Listener deregistered
D/ServiceDetails(16652): Removing ServiceListener
D/BackgroundService(16652): Listener removed
D/ServiceDetails(16652): Removing ServiceConnection
D/ServiceDetails(16652): Close finished
D/BackgroundServicePluginLogic(16652): On Destroy Finish

@aravintha
Copy link
Author

The following code resolved the issue...Your plugin works well the problem is android kitkat version..

Thank you so much...

http://stackoverflow.com/questions/20673403/android-services-start-sticky-does-not-work-on-kitkat?lq=1

// this is for kitkat (nexus 7 tablet) service issue
public void onTaskRemoved(Intent rootIntent) {
// TODO Auto-generated method stub
Intent restartService = new Intent(getApplicationContext(),
this.getClass());
restartService.setPackage(getPackageName());
PendingIntent restartServicePI = PendingIntent.getService(
getApplicationContext(), 1, restartService,
PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmService = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() +1000, restartServicePI);
}
the code is place in BackService.java class..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants