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

BootReceiver not worked #11

Closed
thiagomotta opened this issue May 30, 2014 · 11 comments
Closed

BootReceiver not worked #11

thiagomotta opened this issue May 30, 2014 · 11 comments
Labels

Comments

@thiagomotta
Copy link

Hi,

After restarting the phone, the service is not starting.
Please, can you help me?

Follow my file AndroidManifest.xml
clipboard01

Follow my file service.js

var MyBackgroundService = MyBackgroundService || {};

(function() {
"use strict";

MyBackgroundService = function() {

    var _myService,
        _timer = 15 * 60 * 1000; // 900000

    return {
        Init: function() {
            try {
                var serviceName = 'com.red_folder.phonegap.plugin.backgroundservice.RastreadorService',
                    factory = cordova.require('com.red_folder.phonegap.plugin.backgroundservice.BackgroundService');

                _myService = factory.create(serviceName);

                MyBackgroundService.Go();

            } catch(err) {
            }
        },


        DisplayError: function(data) {
            //alert("display error");
            //alert(tmz.ObjectToString(data));
        },


        EnableTimer: function(data) {

            try {
                if (data.TimerEnabled) {
                    //alert("service now running");
                }

                _myService.enableTimer(
                    _timer, // 900000
                    function(r) { 
                        MyBackgroundService.RegisterForUpdates(r);
                    }, 
                    function(e) {
                        MyBackgroundService.DisplayError(e);
                    }
                );

            } catch(err) {
            }
        },


        GetStatus: function() {

            try {
                _myService.getStatus(
                    function(r) {
                        MyBackgroundService.UpdateHandler(r);
                    }, 
                    function(e) {
                        MyBackgroundService.DisplayError(e);
                    }
                );
            } catch(err) {
            }
        },


        Go: function() {

            try {
                _myService.getStatus(
                    function(r) {

                        MyBackgroundService.StartService(r);


                        if (!r.RegisteredForBootStart) {
                            MyBackgroundService.RegisterForBootStart(r);
                        }

                    }, function(e) {
                        MyBackgroundService.DisplayError(e);
                    }
                );
            } catch(err) {
            }
        },

        RegisterForUpdates: function(data) {

            try {
                /*if (data.RegisteredForUpdates) {
                    return;
                }*/

                _myService.registerForUpdates(
                    function(r) {
                        MyBackgroundService.UpdateHandler(r);
                    },
                    function(e) {
                        MyBackgroundService.DisplayError(e);
                    }
                );
            } catch(err) {
            }
        },


        DeRegisterForUpdates: function() {

            try {
                _myService.deregisterForUpdates(
                    function(r) {
                        MyBackgroundService.UpdateHandler(r);
                    },
                    function(e) {
                        MyBackgroundService.DisplayError(e);
                    }
                );
            } catch(err) {
            }
        },


        RegisterForBootStart: function(data) {

            try {
                if (data.RegisteredForBootStart) {
                    //alert('Service started, timer enabled and service registered for Boot start');
                    return;
                }

                _myService.registerForBootStart(
                    function(r) {
                        MyBackgroundService.UpdateHandler(r);
                    },
                    function(e) {
                        MyBackgroundService.DisplayError(e);
                    }
                );
            } catch(err) {
            }

        },


        DeRegisterForBootStart: function() {

            try {
                _myService.deregisterForBootStart(
                    function(r) {
                        MyBackgroundService.UpdateHandler(r);
                    },
                    function(e) {
                        MyBackgroundService.DisplayError(e);
                    }
                );
            } catch(err) {
            }
        },


        SetConfiguration: function() {

            try {
                var config = {
                    'param1': 'p1',
                    'param2': 'p2'
                };

                _myService.setConfiguration(
                    config,
                    function(r) {
                        //MyBackgroundService.UpdateHandler(r);
                    },
                    function(e) {
                        MyBackgroundService.DisplayError(e);
                    }
                );
            } catch(err) {
            }
        },

        StartService: function(data) {

            try {

                // se o serviço já estiver rodando....
                if (data.ServiceRunning) {
                    MyBackgroundService.EnableTimer(data);
                    MyBackgroundService.SetConfiguration();

                    return;
                }

                _myService.startService(
                    function(r) {
                        MyBackgroundService.EnableTimer(r);
                        MyBackgroundService.SetConfiguration();
                    },
                    function(e) {
                        MyBackgroundService.DisplayError(e);
                    }
                );
            } catch(err) {
            }

        },


        UpdateHandler: function(data) {

            try {
                rpBackground.getLista();
            } catch(err) {
            }
        }
    }
}();

}());

MyBackgroundService.Init()

@thiagomotta thiagomotta changed the title BootService not worked BootReceiver not worked May 30, 2014
@thiagomotta
Copy link
Author

?

@Red-Folder
Copy link
Owner

Sorry, I hadn't noticed your issue.

I'll have to have a dig through the code, but the most obvious thing would be to add some logging to the register function (within BackgroundService.java) if there isn't already any in there. Then add additional logging to the Boot Receiver.

@Red-Folder
Copy link
Owner

Had a quick look at the code;

Within BootReceiver.java add some Log.d statements to show if the onReceive method is actually hit.

Potentially worth also adding Log.d statements to the PropertyHelper.java in getBootServices, addBootService and isBootService. These methods are responsible for saving & retrieving if it should shouldn't start on boot.

Then use the log cat to check firstly that the boot start is being set, then secondly that the BootReceiver is called when the device starts and then that is attempts to start the service.

@thiagomotta
Copy link
Author

Thank you,
I will add these logs and call you back with the result.
I am new to phonegap, how do I get the logs generated by code. Java?

@Red-Folder
Copy link
Owner

Ok - that is a steep learning curve. You need to learn how to develop Android application using Eclipse & Java.

I'd suggest learning these things (searches on google will prove better than me trying to explain them) - assuming you haven;t already:

  1. Install Android Developer kit
  2. Install Eclipse
  3. Find tutorial to build an Android native app using Eclipse
  4. Practice running your native app within the Android emulator
  5. Learn how to attach the "logcat" window in Eclipse to the Android emulator. The logcat screen will display all the log messages from the emulator (or connected physical device if you do correctly).
  6. Look up Log.d java command which will write a debug message to the logcat window.
  7. Learn how to use the filtering within the logcat window to just see your app

Once happy with the above, then come back to your phonegap application and use the skills you've learnt to use the logcat to diagnose the problem within your app. You may see a warning or an error in the logcat if you javascript is badly formed. If not, then you'll want to look for the debug messages (my code is fairly saturated with Log.d statements).

If still getting stuck, then post up the locat results.

@thiagomotta
Copy link
Author

Hi,
see who is giving the error.
Please can you help me?

java.lang.RuntimeException: Unable to instantiate service com.red_folder.phonegap.plugin.backgroundservice.RastreadorService: java.lang.ClassNotFoundException: Didn't find class "com.red_folder.phonegap.plugin.backgroundservice.RastreadorService" on path: DexPathList[[zip file "/data/app/tmz.rastreador-2.apk"],nativeLibraryDirectories=[/data/app-lib/tmz.rastreador-2, /vendor/lib, /system/lib]]
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2559)
at android.app.ActivityThread.access$1800(ActivityThread.java:138)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5050)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:615)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.red_folder.phonegap.plugin.backgroundservice.RastreadorService" on path: DexPathList[[zip file "/data/app/tmz.rastreador-2.apk"],nativeLibraryDirectories=[/data/app-lib/tmz.rastreador-2, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2556)

@Red-Folder
Copy link
Owner

It can't find the service name properly. Is it named correctly?

It this error occurring on boot.

@thiagomotta
Copy link
Author

image

@thiagomotta
Copy link
Author

This application I had first developed in java and eclipse, but then switched to html5. Ellipse in the logcat I've used without problems. I just did not know how to use logcat with phonegap. I just saw on google that I could run a "monitor" command and it will open the Android Debug Monitor

But thanks for your step by step
=)

ps.: Sorry, my english is not very good!

@Red-Folder
Copy link
Owner

Sorry for the delay;

There is a difference in your package name at the top iof RastreaderService.java. Within the file it reads:

com.red_folder.phonegap.plugin.backgroundservice.services

Yet your folder structure and the error in the logcat seems to be expect it to be:

com.red_folder.phonegap.plugin.backgroundservice

Basically, remove the ".services" from your package name

@Red-Folder
Copy link
Owner

No response since June - assumed closed

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

2 participants