0


I'm trying to create an App for mobile phone that talk with another App on my SmartWatch.
The phone App must run in background, of course, in order to send some data to the watch and act in some cases as Proxy.
I wrote a service, that will be started on boot. This service was successfully started on boot and it can initialize the GoogleAPI and so on.
Now, if I start my Main Activity, in order to change some settings, I need the service to send data to the smart watch.
To do that I call some public method of my service and I need to be sure, that the service was started...
The code in my Activity:

Log.v(PROGRAM, "Starting background service");
Intent serviceIntent = new Intent(getApplicationContext(), PilotWatchService.class);
getApplicationContext().startService(serviceIntent);
service = new PilotWatchService();

Log.d(PROGRAM, String.format("System is %s", service.isOnline() ? "online" : "offline"));

as soon I call service.isOnline() the app crashes with the error:

05-19 21:49:55.074  7292  7292 E AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{de.lucabert.pilotwatch/de.lucabert.pilotwatch.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
05-19 21:49:55.074  7292  7292 E AndroidRuntime:        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
05-19 21:49:55.074  7292  7292 E AndroidRuntime:        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
05-19 21:49:55.074  7292  7292 E AndroidRuntime:        at android.app.ActivityThread.-wrap12(ActivityThread.java)
05-19 21:49:55.074  7292  7292 E AndroidRuntime:        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
05-19 21:49:55.074  7292  7292 E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:102)
05-19 21:49:55.074  7292  7292 E AndroidRuntime:        at android.os.Looper.loop(Looper.java:154)
05-19 21:49:55.074  7292  7292 E AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:6119)
05-19 21:49:55.074  7292  7292 E AndroidRuntime:        at java.lang.reflect.Method.invoke(Native Method)
05-19 21:49:55.074  7292  7292 E AndroidRuntime:        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
05-19 21:49:55.074  7292  7292 E AndroidRuntime:        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
05-19 21:49:55.074  7292  7292 E AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
05-19 21:49:55.074  7292  7292 E AndroidRuntime:        at android.content.ContextWrapper.getSystemService(ContextWrapper.java:659)
05-19 21:49:55.074  7292  7292 E AndroidRuntime:        at de.lucabert.pilotwatch.PilotWatchService.isOnline(PilotWatchService.java:282)
05-19 21:49:55.074  7292  7292 E AndroidRuntime:        at de.lucabert.pilotwatch.MainActivity.onCreate(MainActivity.java:116)
05-19 21:49:55.074  7292  7292 E AndroidRuntime:        at android.app.Activity.performCreate(Activity.java:6679)
05-19 21:49:55.074  7292  7292 E AndroidRuntime:        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
05-19 21:49:55.074  7292  7292 E AndroidRuntime:        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
05-19 21:49:55.074  7292  7292 E AndroidRuntime:        ... 9 more
05-19 21:49:55.075  1686  2020 W ActivityManager:   Force finishing activity de.lucabert.pilotwatch/.MainActivity

I really can't understand how I can use the function of my service in my Activity. Can someone help me?

Thanks a lot
Luca

  • Maybe this thread could help https://stackoverflow.com/questions/4844930/how-to-call-methods-of-a-service-from-activity – ali73 May 19 '20 at 20:26
  • Hi ali! Thank you very much! It works! But I have a little problem... as soon as I close the Activity, the Service will be closed, too... I want my Service to continue running in order to serve data to my watch... How can I solve my problem? – Luca Bertoncello May 20 '20 at 18:33
  • I think you want the functionalities of foreground services. I hope this page from documentation can help you: https://developer.android.com/guide/background – ali73 May 20 '20 at 19:58

0 Answers0