Comments (50)
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"
from bgs-core.
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
from bgs-core.
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)
from bgs-core.
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";
from bgs-core.
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)
from bgs-core.
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?
from bgs-core.
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
from bgs-core.
Try changing the lines to:
this.mService = new Intent();
this.mService.setComponent(this.mServiceName);
from bgs-core.
which line should I change with the new one?
the orginal 601 like in the second post?
from bgs-core.
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
from bgs-core.
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.
from bgs-core.
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 ;)
from bgs-core.
Hello,
I have the same issue, someone know the way of solved it?
from bgs-core.
@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]
from bgs-core.
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.
from bgs-core.
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!
from bgs-core.
@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.
from bgs-core.
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>
from bgs-core.
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
from bgs-core.
@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.
from bgs-core.
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?
from bgs-core.
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.
from bgs-core.
Thank you so much niarferuto. I solved the problem with your corrections.
from bgs-core.
Finally I can replicate this
Needed build against V21 (app & cordovalib) and run against V21 emulator.
Now to fix it
from bgs-core.
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
from bgs-core.
Thank you so much Red-Folder! ;)
from bgs-core.
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?
from bgs-core.
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.
from bgs-core.
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.
from bgs-core.
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?
from bgs-core.
Thank you for pointing out.
In case of a solution i will post here also, yes.
from bgs-core.
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..).
- 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;
}
}
-
Change also in AndroidManifest.xml
<service android:name="com.my.app.MyService"> <intent-filter> <action android:name="com.my.app.MyService" /> </intent-filter> </service>
-
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!!!
from bgs-core.
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?
from bgs-core.
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.
from bgs-core.
I've tried it, but it doesn't fix my issue. What android and cordova version are you using?
from bgs-core.
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.
from bgs-core.
@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!
from bgs-core.
@MarkVlamPersie @fovaka1 Thanks for contributing you comments to the issue.
from bgs-core.
@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.
}
from bgs-core.
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.
from bgs-core.
@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
from bgs-core.
@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.
from bgs-core.
@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.
from bgs-core.
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.
from bgs-core.
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.
from bgs-core.
@MarkVlamPersie im trying
all possible solutions but its still not working.
from bgs-core.
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.
from bgs-core.
finally, its solved.
im missing MyService.js file.
from bgs-core.
from bgs-core.
This service Not working in Android 10. any help ?
from bgs-core.
Related Issues (20)
- Error Occurs on startService(success,failed) ;
- Backgound service stops when the app is terminated HOT 2
- creating multiple service class HOT 2
- Does this plugin works with local notifications? HOT 2
- Integrating with NotificationListenerService would it work?
- If Background Service is Running Through Any Local Notification or Alerts?
- Where to put MyService.java in IONIC? Android is unable to find/bind. HOT 2
- Error on Boot
- Its not working in Android 10 HOT 1
- running cordova inside of a service HOT 5
- What is service will not running when app closed ? HOT 8
- It's Possible MyService.java Call Javascript Function ? HOT 3
- ideas to make a service to continue receiving push notifications HOT 2
- Build Android Error HOT 13
- Add method onCleanup to override
- Can I load a third party android library?
- Service starting but restart on exit failing
- Application run but factory is empty
- How to stop a Countdown when the App is backgrounded (Xamarin forms) HOT 2
- Detect the volum up pression in background service
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bgs-core.