-3

I have a doubt regarding using service. Is it necessary to create an object of a service in order to start it or simply declaring a service variable is sufficient?

I want to know that whether a service is started by creating its object or you need to explicitly start the service by calling the start service method?

GPSService trackerService = new GPSService(RunningActivity.this);

Does the above line start the activity? Or it gets started on calling

Intent serviceIntent = new Intent(RunningActivity.this, GPSService.class);
startService(serviceIntent);

All I need is I want to access the service objects in my activity.

Please help.

Thank you.

Riddhi Dudani
  • 107
  • 1
  • 1
  • 10
  • What is the question ?. Look at the android docummentation about service http://developer.android.com/guide/components/services.html – JEY Apr 02 '15 at 14:54
  • I asked this question only because I could not find relevant reply. Please help me with a solution if you have. I want to access methods of the service in my activity. Thank you. – Riddhi Dudani Apr 02 '15 at 17:01

1 Answers1

0

You cannot instantiate a Service. Only the Android framework can do that. You start a service by calling startService().

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • What if I want to access the method of my service e.g. service has a method called startTracking(). Can I use that by writing myservice.startTracking()? What steps I need to follow before doing that? – Riddhi Dudani Apr 04 '15 at 07:36
  • You cannot call methods from your `Service` unless they are `static`. If you need to call methods in your `Service` look at how to bind to a service and use AIDL (remote procedure calls). – David Wasser Apr 04 '15 at 16:50
  • Ok @DavidWasser I will see that. Thank you. – Riddhi Dudani Apr 05 '15 at 05:56