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

Dosen't work on Android 5.0 #18

Closed
apollon0815 opened this issue Oct 24, 2014 · 50 comments
Closed

Dosen't work on Android 5.0 #18

apollon0815 opened this issue Oct 24, 2014 · 50 comments
Assignees
Labels
Milestone

Comments

@apollon0815
Copy link

Hello,

thank you for this wonderful plugin.

It works very well on Android 4.4 and lower.
But when I test it with Eclipse and Android 5 SDK on an Android 5.0 emulator, background service not starting.

I hope you can fix it.

Thank you very much.

Log:

10-24 14:46:58.119: D/BackgroundServicePluginLogic(1206): Start of Execute
10-24 14:46:58.120: D/BackgroundServicePluginLogic(1206): Withing try block
10-24 14:46:58.122: D/BackgroundServicePluginLogic(1206): Finding servicename com.domain.demoapp.MyService
10-24 14:46:58.122: D/BackgroundServicePluginLogic(1206): Services contain 0 records
10-24 14:46:58.123: D/BackgroundServicePluginLogic(1206): Creating new Service Details
10-24 14:46:58.132: W/PluginManager(1206): THREAD WARNING: exec() call to BackgroundServicePlugin.getStatus blocked the main thread for 231ms. Plugin should use CordovaInterface.getThreadPool().
10-24 14:47:00.176: W/PluginManager(1206): THREAD WARNING: exec() call to File.getFile blocked the main thread for 264ms. Plugin should use CordovaInterface.getThreadPool().
10-24 14:47:03.076: D/BackgroundServicePluginLogic(1206): Action = getStatus
10-24 14:47:03.211: D/BackgroundServicePlugin(1206): logicResult = com.red_folder.phonegap.plugin.backgroundservice.BackgroundServicePluginLogic$ExecuteResult@389df6c2
10-24 14:47:03.251: D/BackgroundServicePlugin(1206): Start of transformResult
10-24 14:47:03.266: D/BackgroundServicePlugin(1206): Status is OK
10-24 14:47:03.267: D/BackgroundServicePlugin(1206): We have data
10-24 14:47:03.269: D/BackgroundServicePlugin(1206): End of transformResult
10-24 14:47:03.270: D/BackgroundServicePlugin(1206): pluginResult = org.apache.cordova.PluginResult@187d5cd3
10-24 14:47:03.272: D/BackgroundServicePlugin(1206): pluginResult.getMessage() = {"Success":true,"ErrorCode":0,"ErrorMessage":"","ServiceRunning":false,"RegisteredForBootStart":false,"RegisteredForUpdates":false}
10-24 14:47:03.272: D/BackgroundServicePlugin(1206): Dont keep Callback
10-24 14:47:04.200: D/BackgroundServicePluginLogic(1206): Start of Execute
10-24 14:47:04.201: D/BackgroundServicePluginLogic(1206): Withing try block
10-24 14:47:04.202: D/BackgroundServicePluginLogic(1206): Finding servicename com.domain.demoapp.MyService
10-24 14:47:04.366: D/BackgroundServicePluginLogic(1206): Services contain 1 records
10-24 14:47:04.367: D/BackgroundServicePluginLogic(1206): Found existing Service Details
10-24 14:47:04.368: D/BackgroundServicePluginLogic(1206): Action = startService
10-24 14:47:04.368: D/ServiceDetails(1206): Starting startService
10-24 14:47:04.368: D/ServiceDetails(1206): Attempting to bind to Service
10-24 14:47:04.369: D/ServiceDetails(1206): Starting bindToService
10-24 14:47:04.427: D/ServiceDetails(1206): Attempting to start service
10-24 14:47:04.632: D/ServiceDetails(1206): bindToService failed
10-24 14:47:04.632: D/ServiceDetails(1206): java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.domain.demoapp.MyService }
10-24 14:47:04.632: D/ServiceDetails(1206): at android.app.ContextImpl.validateServiceIntent(ContextImpl.java:1674)
10-24 14:47:04.632: D/ServiceDetails(1206): at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1703)
10-24 14:47:04.632: D/ServiceDetails(1206): at android.app.ContextImpl.startService(ContextImpl.java:1687)
10-24 14:47:04.632: D/ServiceDetails(1206): at android.content.ContextWrapper.startService(ContextWrapper.java:515)
10-24 14:47:04.632: D/ServiceDetails(1206): at com.red_folder.phonegap.plugin.backgroundservice.BackgroundServicePluginLogic$ServiceDetails.bindToService(BackgroundServicePluginLogic.java:604)
10-24 14:47:04.632: D/ServiceDetails(1206): at com.red_folder.phonegap.plugin.backgroundservice.BackgroundServicePluginLogic$ServiceDetails.startService(BackgroundServicePluginLogic.java:335)
10-24 14:47:04.632: D/ServiceDetails(1206): at com.red_folder.phonegap.plugin.backgroundservice.BackgroundServicePluginLogic.execute(BackgroundServicePluginLogic.java:188)
10-24 14:47:04.632: D/ServiceDetails(1206): at com.red_folder.phonegap.plugin.backgroundservice.BackgroundServicePlugin$1.run(BackgroundServicePlugin.java:68)
10-24 14:47:04.632: D/ServiceDetails(1206): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
10-24 14:47:04.632: D/ServiceDetails(1206): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
10-24 14:47:04.632: D/ServiceDetails(1206): at java.lang.Thread.run(Thread.java:818)
10-24 14:47:04.653: D/ServiceDetails(1206): Finished bindToService
10-24 14:47:04.654: D/ServiceDetails(1206): Bind Failed
10-24 14:47:04.655: D/ServiceDetails(1206): Finished startService
10-24 14:47:04.656: D/BackgroundServicePlugin(1206): logicResult = com.red_folder.phonegap.plugin.backgroundservice.BackgroundServicePluginLogic$ExecuteResult@3ddb0010
10-24 14:47:04.656: D/BackgroundServicePlugin(1206): Start of transformResult
10-24 14:47:04.656: D/BackgroundServicePlugin(1206): Status is ERROR
10-24 14:47:04.657: D/BackgroundServicePlugin(1206): We have data
10-24 14:47:04.657: D/BackgroundServicePlugin(1206): End of transformResult
10-24 14:47:04.657: D/BackgroundServicePlugin(1206): pluginResult = org.apache.cordova.PluginResult@2ca41709
10-24 14:47:04.658: D/BackgroundServicePlugin(1206): pluginResult.getMessage() = {"Success":false,"ErrorCode":-4,"ErrorMessage":"Plugin unable to bind to background service","ServiceRunning":false,"RegisteredForBootStart":false,"RegisteredForUpdates":false}
10-24 14:47:04.658: D/BackgroundServicePlugin(1206): Dont keep Callback

@Red-Folder
Copy link
Owner

Amend the BackgroundServicePluginLogic.java, from (line 601):

this.mService = new Intent(this.mServiceName);

to

String packageName = "com.red_folder.phonegap.plugin.backgroundservice.sample";
String className = "MyService";
ComponentName component = new ComponentName(packageName, className);
Intent implicitIntent = new Intent(this.mServiceName);
this.mService = new Intent(implicitIntent);
this.mService.setComponent(component);

Unable to test at the moment, but I'll have a go when I've sorted my development environment. The code could definitely be better, but (fingers crossed) this does the trick for you.

Very keen to hear how well it works (or doesn't)

@Red-Folder Red-Folder added the bug label Oct 27, 2014
@Red-Folder
Copy link
Owner

Sorry ... should have mentioned, amend the two variables to reflect your package and class name:

String packageName = "com.red_folder.phonegap.plugin.backgroundservice.sample";
String className = "MyService";

@apollon0815
Copy link
Author

Hello Mark,

thank you for your quick answer. but your trick unfortunately doesn't fix the problem :(

Here is the log:

10-27 22:42:46.710: D/BackgroundServicePluginLogic(858): Start of Execute
10-27 22:42:46.711: D/BackgroundServicePluginLogic(858): Withing try block
10-27 22:42:46.712: D/BackgroundServicePluginLogic(858): Finding servicename com.domain.demoapp.MyService
10-27 22:42:46.721: W/PluginManager(858): THREAD WARNING: exec() call to BackgroundServicePlugin.getStatus blocked the main thread for 24ms. Plugin should use CordovaInterface.getThreadPool().
10-27 22:42:46.764: D/BackgroundServicePluginLogic(858): Services contain 0 records
10-27 22:42:46.765: D/BackgroundServicePluginLogic(858): Creating new Service Details
10-27 22:42:47.733: D/BackgroundServicePluginLogic(858): Action = getStatus
10-27 22:43:02.224: D/BackgroundServicePlugin(858): logicResult = com.red_folder.phonegap.plugin.backgroundservice.BackgroundServicePluginLogic$ExecuteResult@1eee5300
10-27 22:43:02.225: D/BackgroundServicePlugin(858): Start of transformResult
10-27 22:43:02.226: D/BackgroundServicePlugin(858): Status is OK
10-27 22:43:02.227: D/BackgroundServicePlugin(858): We have data
10-27 22:43:02.228: D/BackgroundServicePlugin(858): End of transformResult
10-27 22:43:02.229: D/BackgroundServicePlugin(858): pluginResult = org.apache.cordova.PluginResult@3ac38739
10-27 22:43:02.230: D/BackgroundServicePlugin(858): pluginResult.getMessage() = {"Success":true,"ErrorCode":0,"ErrorMessage":"","ServiceRunning":false,"RegisteredForBootStart":false,"RegisteredForUpdates":false}
10-27 22:43:02.231: D/BackgroundServicePlugin(858): Dont keep Callback
10-27 22:43:02.464: D/BackgroundServicePluginLogic(858): Start of Execute
10-27 22:43:02.464: D/BackgroundServicePluginLogic(858): Withing try block
10-27 22:43:02.466: D/BackgroundServicePluginLogic(858): Finding servicename com.domain.demoapp.MyService
10-27 22:43:02.466: D/BackgroundServicePluginLogic(858): Services contain 1 records
10-27 22:43:02.479: D/BackgroundServicePluginLogic(858): Found existing Service Details
10-27 22:43:02.480: D/BackgroundServicePluginLogic(858): Action = startService
10-27 22:43:02.481: D/ServiceDetails(858): Starting startService
10-27 22:43:02.481: D/ServiceDetails(858): Attempting to bind to Service
10-27 22:43:02.482: D/ServiceDetails(858): Starting bindToService
10-27 22:43:02.483: D/ServiceDetails(858): Attempting to start service
10-27 22:43:02.497: D/ServiceDetails(858): Attempting to bind to service
10-27 22:43:02.649: D/ServiceDetails(858): Finished bindToService
10-27 22:43:02.650: D/ServiceDetails(858): Bind Failed
10-27 22:43:02.652: D/ServiceDetails(858): Finished startService
10-27 22:43:02.652: D/BackgroundServicePlugin(858): logicResult = com.red_folder.phonegap.plugin.backgroundservice.BackgroundServicePluginLogic$ExecuteResult@1178d6df
10-27 22:43:02.703: D/BackgroundServicePlugin(858): Start of transformResult
10-27 22:43:02.705: D/BackgroundServicePlugin(858): Status is ERROR
10-27 22:43:02.706: D/BackgroundServicePlugin(858): We have data
10-27 22:43:02.708: D/BackgroundServicePlugin(858): End of transformResult
10-27 22:43:02.708: D/BackgroundServicePlugin(858): pluginResult = org.apache.cordova.PluginResult@11659f2c
10-27 22:43:02.709: D/BackgroundServicePlugin(858): pluginResult.getMessage() = {"Success":false,"ErrorCode":-4,"ErrorMessage":"Plugin unable to bind to background service","ServiceRunning":false,"RegisteredForBootStart":false,"RegisteredForUpdates":false}
10-27 22:43:02.709: D/BackgroundServicePlugin(858): Dont keep Callback
10-27 22:43:04.114: I/art(858): Thread[2,tid=864,WaitingInMainSignalCatcherLoop,Thread*=0xaf088400,peer=0x12c00080,"Signal Catcher"]: reacting to signal 3
10-27 22:43:17.401: I/art(858): Wrote stack traces to '/data/anr/traces.txt'
10-27 22:43:17.426: D/CordovaNetworkManager(858): Connection Type: 3g
10-27 22:43:17.426: D/CordovaNetworkManager(858): Connection Extra Info: epc.tmobile.com
10-27 22:43:17.451: D/CordovaActivity(858): onMessage(networkconnection,3g)
10-27 22:43:18.777: D/CordovaActivity(858): CordovaActivity.onDestroy()
10-27 22:43:18.839: D/CordovaWebView(858): >>> loadUrlNow()
10-27 22:43:18.902: D/BackgroundServicePluginLogic(858): On Destroy Start
10-27 22:43:18.934: D/BackgroundServicePluginLogic(858): Checking for services
10-27 22:43:18.934: D/BackgroundServicePluginLogic(858): Found services
10-27 22:43:18.936: D/BackgroundServicePluginLogic(858): Calling service.close()
10-27 22:43:18.937: D/ServiceDetails(858): Close called
10-27 22:43:18.938: D/ServiceDetails(858): Removing ServiceListener
10-27 22:43:18.997: D/ServiceDetails(858): close failed
10-27 22:43:18.997: D/ServiceDetails(858): java.lang.NullPointerException: Attempt to invoke interface method 'void com.red_folder.phonegap.plugin.backgroundservice.BackgroundServiceApi.removeListener(com.red_folder.phonegap.plugin.backgroundservice.BackgroundServiceListener)' on a null object reference
10-27 22:43:18.997: D/ServiceDetails(858): at com.red_folder.phonegap.plugin.backgroundservice.BackgroundServicePluginLogic$ServiceDetails.close(BackgroundServicePluginLogic.java:555)
10-27 22:43:18.997: D/ServiceDetails(858): at com.red_folder.phonegap.plugin.backgroundservice.BackgroundServicePluginLogic.onDestroy(BackgroundServicePluginLogic.java:246)
10-27 22:43:18.997: D/ServiceDetails(858): at com.red_folder.phonegap.plugin.backgroundservice.BackgroundServicePlugin.onDestroy(BackgroundServicePlugin.java:102)
10-27 22:43:18.997: D/ServiceDetails(858): at org.apache.cordova.PluginManager.onDestroy(PluginManager.java:326)
10-27 22:43:18.997: D/ServiceDetails(858): at org.apache.cordova.CordovaWebView.handleDestroy(CordovaWebView.java:833)
10-27 22:43:18.997: D/ServiceDetails(858): at org.apache.cordova.CordovaActivity.onDestroy(CordovaActivity.java:774)
10-27 22:43:18.997: D/ServiceDetails(858): at android.app.Activity.performDestroy(Activity.java:6112)
10-27 22:43:18.997: D/ServiceDetails(858): at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1140)
10-27 22:43:18.997: D/ServiceDetails(858): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3658)
10-27 22:43:18.997: D/ServiceDetails(858): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3689)
10-27 22:43:18.997: D/ServiceDetails(858): at android.app.ActivityThread.access$1400(ActivityThread.java:144)
10-27 22:43:18.997: D/ServiceDetails(858): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1332)
10-27 22:43:18.997: D/ServiceDetails(858): at android.os.Handler.dispatchMessage(Handler.java:102)
10-27 22:43:18.997: D/ServiceDetails(858): at android.os.Looper.loop(Looper.java:135)
10-27 22:43:18.997: D/ServiceDetails(858): at android.app.ActivityThread.main(ActivityThread.java:5221)
10-27 22:43:18.997: D/ServiceDetails(858): at java.lang.reflect.Method.invoke(Native Method)
10-27 22:43:18.997: D/ServiceDetails(858): at java.lang.reflect.Method.invoke(Method.java:372)
10-27 22:43:18.997: D/ServiceDetails(858): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
10-27 22:43:18.997: D/ServiceDetails(858): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
10-27 22:43:18.999: D/ServiceDetails(858): Ignoring exception - will continue
10-27 22:43:19.000: D/ServiceDetails(858): Close finished
10-27 22:43:19.000: D/BackgroundServicePluginLogic(858): On Destroy Finish
10-27 22:43:19.202: E/WindowManager(858): android.view.WindowLeaked: Activity com.domain.demoapp.DemoApp has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{3d7ecde2 V.E..... R.....ID 0,0-480,387} that was originally added here
10-27 22:43:19.202: E/WindowManager(858): at android.view.ViewRootImpl.(ViewRootImpl.java:363)
10-27 22:43:19.202: E/WindowManager(858): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:261)
10-27 22:43:19.202: E/WindowManager(858): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
10-27 22:43:19.202: E/WindowManager(858): at android.app.Dialog.show(Dialog.java:298)
10-27 22:43:19.202: E/WindowManager(858): at android.app.AlertDialog$Builder.show(AlertDialog.java:987)
10-27 22:43:19.202: E/WindowManager(858): at org.apache.cordova.CordovaActivity$4.run(CordovaActivity.java:988)
10-27 22:43:19.202: E/WindowManager(858): at android.app.Activity.runOnUiThread(Activity.java:5247)
10-27 22:43:19.202: E/WindowManager(858): at org.apache.cordova.CordovaActivity.displayError(CordovaActivity.java:971)
10-27 22:43:19.202: E/WindowManager(858): at org.apache.cordova.CordovaActivity$3.run(CordovaActivity.java:954)
10-27 22:43:19.202: E/WindowManager(858): at android.app.Activity.runOnUiThread(Activity.java:5247)
10-27 22:43:19.202: E/WindowManager(858): at org.apache.cordova.CordovaActivity.onReceivedError(CordovaActivity.java:950)
10-27 22:43:19.202: E/WindowManager(858): at org.apache.cordova.CordovaActivity.onMessage(CordovaActivity.java:1172)
10-27 22:43:19.202: E/WindowManager(858): at org.apache.cordova.PluginManager.postMessage(PluginManager.java:339)
10-27 22:43:19.202: E/WindowManager(858): at org.apache.cordova.CordovaWebView.postMessage(CordovaWebView.java:543)
10-27 22:43:19.202: E/WindowManager(858): at org.apache.cordova.CordovaWebViewClient.onReceivedError(CordovaWebViewClient.java:352)
10-27 22:43:19.202: E/WindowManager(858): at org.apache.cordova.CordovaWebView$2.run(CordovaWebView.java:440)
10-27 22:43:19.202: E/WindowManager(858): at android.os.Handler.handleCallback(Handler.java:739)
10-27 22:43:19.202: E/WindowManager(858): at android.os.Handler.dispatchMessage(Handler.java:95)
10-27 22:43:19.202: E/WindowManager(858): at android.os.Looper.loop(Looper.java:135)
10-27 22:43:19.202: E/WindowManager(858): at android.app.ActivityThread.main(ActivityThread.java:5221)
10-27 22:43:19.202: E/WindowManager(858): at java.lang.reflect.Method.invoke(Native Method)
10-27 22:43:19.202: E/WindowManager(858): at java.lang.reflect.Method.invoke(Method.java:372)
10-27 22:43:19.202: E/WindowManager(858): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
10-27 22:43:19.202: E/WindowManager(858): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
10-27 22:43:19.436: E/ActivityThread(858): Activity com.domain.demoapp.DemoApp has leaked ServiceConnection com.red_folder.phonegap.plugin.backgroundservice.BackgroundServicePluginLogic$ServiceDetails$1@278a217e that was originally bound here
10-27 22:43:19.436: E/ActivityThread(858): android.app.ServiceConnectionLeaked: Activity com.domain.demoapp.DemoApp has leaked ServiceConnection com.red_folder.phonegap.plugin.backgroundservice.BackgroundServicePluginLogic$ServiceDetails$1@278a217e that was originally bound here
10-27 22:43:19.436: E/ActivityThread(858): at android.app.LoadedApk$ServiceDispatcher.(LoadedApk.java:1070)
10-27 22:43:19.436: E/ActivityThread(858): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:964)
10-27 22:43:19.436: E/ActivityThread(858): at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1768)
10-27 22:43:19.436: E/ActivityThread(858): at android.app.ContextImpl.bindService(ContextImpl.java:1751)
10-27 22:43:19.436: E/ActivityThread(858): at android.content.ContextWrapper.bindService(ContextWrapper.java:538)
10-27 22:43:19.436: E/ActivityThread(858): at com.red_folder.phonegap.plugin.backgroundservice.BackgroundServicePluginLogic$ServiceDetails.bindToService(BackgroundServicePluginLogic.java:612)
10-27 22:43:19.436: E/ActivityThread(858): at com.red_folder.phonegap.plugin.backgroundservice.BackgroundServicePluginLogic$ServiceDetails.startService(BackgroundServicePluginLogic.java:335)
10-27 22:43:19.436: E/ActivityThread(858): at com.red_folder.phonegap.plugin.backgroundservice.BackgroundServicePluginLogic.execute(BackgroundServicePluginLogic.java:188)
10-27 22:43:19.436: E/ActivityThread(858): at com.red_folder.phonegap.plugin.backgroundservice.BackgroundServicePlugin$1.run(BackgroundServicePlugin.java:68)
10-27 22:43:19.436: E/ActivityThread(858): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
10-27 22:43:19.436: E/ActivityThread(858): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
10-27 22:43:19.436: E/ActivityThread(858): at java.lang.Thread.run(Thread.java:818)
10-27 22:43:21.763: D/CordovaWebViewClient(858): onPageFinished(about:blank)
10-27 22:43:21.763: D/CordovaActivity(858): onMessage(onPageFinished,about:blank)

@Red-Folder
Copy link
Owner

We seem to be getting a different error. Original problem was failing on bind ... now we seem to be getting further.

Can I just check, what values are you using for packageName & className?

@apollon0815
Copy link
Author

Here are the names

String packageName = "com.domain.demoapp";
String className = "MyService";

=> the MyService (in my case the push service) is under the package "com.domain.demoapp" in the file MyService.java

With Android 4.4 and lower it works...

When I build a empty app like in your tutorial, its the same problem under Android 5

@Red-Folder
Copy link
Owner

Try changing the lines to:

this.mService = new Intent();
this.mService.setComponent(this.mServiceName);

@apollon0815
Copy link
Author

which line should I change with the new one?
the orginal 601 like in the second post?

@apollon0815
Copy link
Author

I replaced line 601 (original file) with your two lines

Error:
The method setComponent(ComponentName) in the type Intent is not applicable for the arguments (String) BackgroundServicePluginLogic.java line 602

@Red-Folder
Copy link
Owner

Sorry - my fault. I misread the Android documents.

I hope to have my development environment working tonight (spent the last two evenings getting it up to Android 5). So will investigate this issue for fully.

@apollon0815
Copy link
Author

Yes I know, to build a good working development environment is sometimes tricky. I waiting eagerly to test the patched plugin.

I would also like to thank you for the speedy response ;)

@niarferuto
Copy link

Hello,
I have the same issue, someone know the way of solved it?

@niarferuto
Copy link

I resolved by changing the line
this.mService = new Intent(this.mServiceName);
to
this.mService = new Intent(this.mContext, MyService.class);

Tested in nexus 4 with android 5.0 and Sony Xperia L with android 4.2.2

But when i reboot the phone in the sony keep the service running but in the nexus the service status is "Not running"

@niarferuto
Copy link

For the reboot problem i resolved changing this lines in BootReceiver.java

for (int i = 0; i < serviceList.length; i++)
{
Intent serviceIntent = new Intent(serviceList[i]);
context.startService(serviceIntent);
}

to

Intent serviceIntent = new Intent(context, MyService.class);
context.startService(serviceIntent);

I suppose that this changes only work if i have only that service

@Red-Folder
Copy link
Owner

@niarferuto Thank you so much for the providing you fix. This is greatly appreciated.

I'm still struggling to rebuild a development environment that works. I cannot get the 5.0 emulator to start and interest with Eclipse reliably. My development kit may just be getting too old for it now - which is causing me problems maintaining the plugin (as seen by how long this issue has been unaddressed).

Regarding your last comment; I would expect that you can use reflection to MyService.class from the serviceList[i]

@Red-Folder
Copy link
Owner

Finally have my development environment working again (just)

I'm currently unable to replicate the issue. I've created a Cordova Project, added the bgs-sample plugin (which includes bgs-core), set the Android target to 5.0.1 and run on a 5.0.1 emulator. I'm not getting any errors.

@apollon0815
Copy link
Author

It's crazy that it work for you, but on my system the problem is still there.
I have updated my development platform, but it's the same.

I use the newest version of eclipse on osx and like you Android target and emulator 5.0.1. The cordova version is 3.1.0, because the compatibility with other plugins. The service is under a other path. It worked like charmed under Android 5 with the changes of niarferuto, when I write the absolute path of the class like "com.domain.demoapp.MyService.class"

When I test it with real phones and android 5 it's like in the emulator, without the changes of niarferuto the service does not start.

@niarferuto
The patch does the job. Thank you!

@Red-Folder
Copy link
Owner

@apollon0815 Could you post the contents of your AndroidManifest.xml (specifically the service section).

I expect to implement the code from @niarferuto - but my development environment is proving to be a real struggle.

@apollon0815
Copy link
Author

Hello Red ;)

here is the content of my xml file:

<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="1.0.0" android:windowSoftInputMode="adjustPan" package="com.domain.demoapp" xmlns:android="http://schemas.android.com/apk/res/android">
    <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
    <application android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/app_name" android:name="DemoApp" android:theme="@android:style/Theme.Black.NoTitleBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <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.domain.demoapp.PushService">
         <intent-filter>
          <action android:name="com.domain.demoapp.PushService" />
         </intent-filter>
        </service>
    </application>
    <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="21" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.BROADCAST_STICKY" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>

@lylepratt
Copy link

You need to make sure you set your targetSdkVersion to 21. I'm getting this same error.

This may be helpful:
http://blog.android-develop.com/2014/10/android-l-api-21-javalangillegalargumen.html

@Red-Folder
Copy link
Owner

@lylepratt I'd seen that article - but thanks anyway

I'll try with the targetSdkVersion, see if that allows me to recreate. I'm rebuilding my development environment (again) in an attempt to actually be able to run an emulator properly - so hopefully I can get back on to this soon.

I've a few other bugs I'd like to fix - along with some refactor work - hopefully get this done over the Christmas period.

@lylepratt
Copy link

I'm to the point where I am forced to update to sdk version 21. Now that I have, i'm back to this problem. Anyone have any advice?

@hectorg83
Copy link

Hi, I am working with the last version of this plugin and the last version of apache cordova.

I have a problem with this plugin in Android 5, Nexus 5.

I tested the plugin with the example https://github.com/Red-Folder/bgs-core/wiki/Using-the-MyService-Sample and It doesn´t work.

When I press the button Service, I receive the next error: Plugin unable to bind background service.

The same example works in android 4.4 (Nexus 5), adroid ice-cream and android jelly bean.

@hectorg83
Copy link

Thank you so much niarferuto. I solved the problem with your corrections.

@Red-Folder Red-Folder self-assigned this Mar 11, 2015
@Red-Folder Red-Folder added this to the March 2015 milestone Mar 11, 2015
@Red-Folder
Copy link
Owner

Finally I can replicate this

Needed build against V21 (app & cordovalib) and run against V21 emulator.

Now to fix it

@Red-Folder
Copy link
Owner

Fixed in commit 167a4f7

Tested on emulator versions:

  • Android 2.3.3/ Version 10
  • Android 4.4.2/ Version 19
  • Android 5.0.1/ Version 21

@hectorg83
Copy link

Thank you so much Red-Folder! ;)

@alexandreetf
Copy link

Hi!
I'm trying to run in Android 5, but the service doesn't start on boot. When i open the app run perfectly.
But on boot, the console log shows:
04-04 01:30:06.665: I/ActivityManager(937): Waited long enough for: ServiceRecord{18f53d58 u0 com.ionicframework.back337261/com.red_folder.phonegap.plugin.backgroundservice.sample.MyService}

Someone could help me?

@MarkVP10
Copy link

MarkVP10 commented May 12, 2016

Hello Red-Folder,

I'm currently trying to use your tutorial to create a background service for my android application. I have used the exact same names as you have, because i wanted to get it to work in the first place. I get the exact same errormessage as mentioned above.

Plugin unable to bind to background service.

I've tried all the fixes mentioned above as well, but it does not fix the problem. I have been stuck with this problem for a few days now and i now hope you can help me any further, any chance you see what the problem is?

The code i currently have on line 601 in the file BackgroundServicePluginLogic.java is, as mentioned above by niarferuto:

Class<?> serviceClass = ReflectionHelper.LoadClass(this.mServiceName);
this.mService = new Intent(this.mContext, MyService.class);

i've also tried the solutions you mentioned. But they don't fix my problem.

@fovaka1
Copy link

fovaka1 commented May 13, 2016

Hello to everyone,

i am facing the same problem with MarkVlamPersie.

Is there a workaround?

The version of BackgroundServicePluginLogic.java that i have is already corrected according to above fixes, but still my app says "Plugin unable to bind to background service"

Thank you very much.

@MarkVP10
Copy link

Fovaka1,

I have opened a thread on Stackoverflow for this. Since JugnuPathak who opened a new issue seems to face the same problem. Would be nice if you could upvote this.

Also if you do run into a solution can you post it here?

@fovaka1
Copy link

fovaka1 commented May 13, 2016

Thank you for pointing out.

In case of a solution i will post here also, yes.

@fovaka1
Copy link

fovaka1 commented May 13, 2016

Found it!!!!:)

Firstly check that you have

package com.red_folder.phonegap.plugin.backgroundservice.BackgroundService;

inside MyService.java on top.

I did not notice that was missing.

After you do that go to file com.red_folder.phonegap.plugin.backgroundservice.BackgroundService.BackgroundServicePluginLogic.java

line 163 and change

String serviceName = data.getString(0);

to

String serviceName = "com.red_folder.phonegap.plugin.backgroundservice.BackgroundService.MyService";

Otherwise i followed below steps(i did this one first and is working but i think the above could be also a solution, so as not to change files, positions etc..).

  1. move MyService.java from com.red_folder.phonegap.plugin.backgroundservice.BackgroundService to com.my.app

2.MyService.java is the following

package com.my.app;

import android.util.Log;

import com.red_folder.phonegap.plugin.backgroundservice.BackgroundService;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MyService extends BackgroundService {
    @Override
    protected JSONObject doWork() {
        JSONObject result = new JSONObject();

        try {
            // Following three lines simply produce a text string with Hello World and the date & time (UK format)
            SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
            String now = df.format(new Date(System.currentTimeMillis()));
            String msg = "Hello World - its currently " + now;

            // We output the message to the logical
            Log.d("MyService", msg);

            // We also provide the same message in our JSON Result
            result.put("Message", msg);
        } catch (JSONException e) {
            // In production code, you would have some exception handling here
        }

        return result;
    }

    @Override
    protected JSONObject getConfig() {
        return null;
    }

    @Override
    protected void setConfig(JSONObject config) {

    }

    @Override
    protected JSONObject initialiseLatestResult() {
        return null;
    }

}
  1. Change also in AndroidManifest.xml

    <service android:name="com.my.app.MyService">
        <intent-filter>
            <action android:name="com.my.app.MyService" />
        </intent-filter>
    </service>
    
  2. Go to file com.red_folder.phonegap.plugin.backgroundservice.BackgroundService.BackgroundServicePluginLogic.java

line 163 and change

String serviceName = data.getString(0);

to

String serviceName = "com.my.app.MyService";

i hope to help you!!!

@MarkVP10
Copy link

Hmmm,

I've tried your solution, and it doesn't work for me. but i think i haven't implemented it in the right way.
My directories look as follows:
com.red_folder.phonegap.plugin.backgroundservice

--------------->aidl
--------------------->(some files here)
--------------->sample
--------------------->MyService.java
--------------->src
--------------------->android
------------------------------>BackgroundService.java
------------------------------>BackgroundServicePluginLogi.java 
------------------------------>(the other included files)
--------------->www
--------------------->backgroundService.js

Now my MyService.java class looks like:

package com.red_folder.phonegap.plugin.backgroundservice.sample;

import com.red_folder.phonegap.plugin.backgroundservice.BackgroundService;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MyService extends BackgroundService {
//code from tutorial here
}

You said to set the package to:
package com.red_folder.phonegap.plugin.backgroundservice.BackgroundService;

I've put that as the package as well as on top of the package. But neither fix the problem.

And my android manifest looks like:

        <service android:name="com.red_folder.phonegap.plugin.backgroundservice.sample.MyService">
            <intent-filter>
                <action android:name="com.red_folder.phonegap.plugin.backgroundservice.sample.MyService" />
            </intent-filter>
        </service>

Then as you said, i changed line 163 in BackgroundServicePluginLogic.java to:
String serviceName = "com.red_folder.phonegap.plugin.backgroundservice.sample.MyService";

Is there anything i've done wrong or missed from you solution?

@fovaka1
Copy link

fovaka1 commented May 13, 2016

Try this,

get your MyService.java file and put it inside your main directory

com.my.app

change package of course accordingly.

change manifest accordingly

leave line 163 in BackgroundServicePluginLogic.java as was before and just put this inside app.js
inside ready function....

var serviceName = 'com.my.app.MyService';
var factory = cordova.require('com.red_folder.phonegap.plugin.backgroundservice.BackgroundService')
myService = factory.create(serviceName);
go();
......
The rest of the functions are the same here.

@MarkVP10
Copy link

MarkVP10 commented May 13, 2016

I've tried it, but it doesn't fix my issue. What android and cordova version are you using?

@fovaka1
Copy link

fovaka1 commented May 13, 2016

Tested with Android Devices 4.3 and 5.1 and both work just fine.

Cordova version 6.1.1

Take android folder (IONIC) and open it with Android Studio.

Try the above changes again.

@MarkVP10
Copy link

@fovaka1 it wont work for me. I'll try starting a new project and see if it works there. Thanks for your clear instructions though!

@Red-Folder
Copy link
Owner

@MarkVlamPersie @fovaka1 Thanks for contributing you comments to the issue.

@MarkVP10
Copy link

@Red-Folder unfortunately it still won't work for me. Do you have any suggestions on changes that could fix it?

I have followed your instructions from the tutorial and i've tried it multiple times but it wont work. I've tried multiple fixes my self. Few things i have noticed so far are:

  • i can't find MyService.java in the build version after putting:
    $ ionic build android in the command line. Is this a problem? If so, where should it be and how can i fix this?
  • MyService,Java is not defined in plugin.xml as a source file. I've tried adding it myself like this:

<source-file src="sample/MyService.java" target-dir="src/com/red_folder/phonegap/plugin/backgroundservice" />
but it does not help.

  • backgroundService.js from the plugin is different after building it. Namely on top of it in the plugin-version i starts with:
//a large comment
function BackgroundServiceFactory() { }

BackgroundServiceFactory.prototype.create = function (serviceName) {
//Some code here
}

Then after building, i looked up the same file in the platforms/android directory and found the file like this:

cordova.define("com.red_folder.phonegap.plugin.backgroundservice.BackgroundService", function(require, exports, module) {
//and then the above mentioned code inbetween the brackets.
}

@MarkVP10
Copy link

I've now finally got it to work. The problem was that MyService.java was excluded from the build when i used the command: ionic build android. I have fixed this problem litteraly by just putting MyService.java in the build directory with the other java-files.

Thanks for all the help.

@Red-Folder
Copy link
Owner

@MarkVlamPersie Great news that its working. Could I ask a favour please

Could you review the documention -> https://github.com/Red-Folder/bgs-core/wiki/Using-the-MyService-Sample

If there are changes you feel that would be appropriate given you experience it would be much appreciated. Thanks

@MarkVP10
Copy link

@Red-Folder Sure! I will take a look at it this evening if i can find the time. If not i'll do it tomorrow.

@MarkVP10
Copy link

@Red-Folder Firstly, sorry for very delayed response.

I haven't worked with that documentation since i used the tutorial to implement the backgroundservice in my application. I think this documentation is very clear. The only thing that i could possibly think of to edit to the tutorial documentation is to provide the package for you example in code lines. In step 2 something similar to:
package com.red_folder.phonegap.plugin.backgroundservice.sample;

Also the documentation is still up-to-date and the plugin should work if you implement it correctly(, unlike me).

That's basicly all i can say, since the documentation is very clear and most of the code quite self explanatory.

@nitzpb
Copy link

nitzpb commented Jun 16, 2017

hi, Mark can u help me please.
i'm stuck at same point where u had before.

I've tried your solution, and it doesn't work for me. but i think i haven't implemented it in the right way.
My directories look as follows:
com.red_folder.phonegap.plugin.backgroundservice

--------------->aidl
--------------------->(some files here)
--------------->sample
--------------------->MyService.java
--------------->src
--------------------->android
------------------------------>BackgroundService.java
------------------------------>BackgroundServicePluginLogi.java
------------------------------>(the other included files)
--------------->www
--------------------->backgroundService.js

Now my MyService.java class looks like:

package com.red_folder.phonegap.plugin.backgroundservice.sample;

import com.red_folder.phonegap.plugin.backgroundservice.BackgroundService;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MyService extends BackgroundService {
//code from tutorial here
}
You said to set the package to:
package com.red_folder.phonegap.plugin.backgroundservice.BackgroundService;

I've put that as the package as well as on top of the package. But neither fix the problem.

And my android manifest looks like:

    <service android:name="com.red_folder.phonegap.plugin.backgroundservice.sample.MyService">
        <intent-filter>
            <action android:name="com.red_folder.phonegap.plugin.backgroundservice.sample.MyService" />
        </intent-filter>
    </service>

Then as you said, i changed line 163 in BackgroundServicePluginLogic.java to:
String serviceName = "com.red_folder.phonegap.plugin.backgroundservice.sample.MyService";

Is there anything i've done wrong or missed from you solution?

what i go next from this position can u guide me.??
im getting error report on console is-

Error: Plugin unable to bind to background service

and
java Script Alert come from code is-

ErrorCode: -4, Success: false, RegisteredForUpdates:false,RegisteredForBootStart:false,ErrorMessage: Plugin unable to to bind to background service,ServiceRunning: false

i spend couple of days on this to solve the problem.

@nitzpb
Copy link

nitzpb commented Jun 16, 2017

Hey MarkVlamPersie,
can u explain how u do this.?

I've now finally got it to work. The problem was that MyService.java was excluded from the build when i used the command: ionic build android. I have fixed this problem litteraly by just putting MyService.java in the build directory with the other java-files.

@nitzpb
Copy link

nitzpb commented Jun 16, 2017

@MarkVlamPersie im trying

all possible solutions but its still not working.

@MarkVP10
Copy link

MarkVP10 commented Jun 16, 2017

Hey @nitzpb ,

I have not worked with this plugin for about a year now, so i find it a bit hard to remember everything i did since i do not have the source code itself anymore. You seem to have taken the right steps.

My problem was that i when i used the build command a few Java files were excluded. What you need to do is take a look in the build folder after you have used the build command and see if all the Java files that are in your project also are in the build folder. If some are missing you need to copy those java files from your project to the build folder. E.g. for me the MyService.java was excluded from the build and it was not in the build folder after using the build command, so i copied the MyService.java from the source folder to the build folder. That was what solved the problem for me.

I hope this helps you with your problem.

@nitzpb
Copy link

nitzpb commented Jun 17, 2017

finally, its solved.
im missing MyService.js file.

@nitzpb
Copy link

nitzpb commented Jun 19, 2017 via email

@deepakjsr7979
Copy link

This service Not working in Android 10. any help ?

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

10 participants