Starting a service from an application class

Possible duplicate:
Can I start the service from the # onCreate () application?

Can I start a service from an application class?

I want to start the service when my application starts. So, instead of starting the service from the initial action, can I use the application class for this? Thanks.

What I tried is the Inside application class:

public void onCreate() { super.onCreate(); Log.i("Logger", "Service Starting"); Intent errorLoggerService = new Intent(getApplicationContext(), ErrorLoggerService.class); getApplicationContext().startService(errorLoggerService); } 
+7
source share
2 answers

Yes you can do this:

 public class MyApplication extends Application { public void onCreate() { super.onCreate(); startService(new Intent(this, MyService.class)); } } 

EDIT Do not call getApplicationContext (), just call startService (). Also, make sure you declare the service in your manifest.

+15
source

Yes, you can use Intent to start the service,

 Intent serviceIntent = new Intent(); serviceIntent.setAction("packagename.serviceName"); context.startService(serviceIntent); 
-3
source

All Articles